Session Changed In Webhook

孤人 提交于 2020-04-18 04:01:30

问题


Im building a webshop using Stripe and .Net, and I'm having some problems with the session not persisting in my webhook and as a result the webhook is unable to get cart or customer information.

My add to cart method looks like this:

            var cartList = new List<CartProduct>();

            var stringObj = _session.GetString("cart");

            /* Logic for deserializing stringObj into List<CartProduct> */

            stringObj = JsonConvert.SerializeObject(cartList);

            _session.SetString("cart", stringObj);

If I look at _session I can see it has an ID. However, when I try to get the cart from my webhook, the session ID has changed and as a result the cart is null.

My get cart method fires at charge.succeeded using the webhook and starts of like this:

            var cart = _session.GetString("cart"); // Returns null because session has changed

            var cartList = JsonConvert.DeserializeObject<List<CartProduct>>(cart); // Exception thrown due to cart being null

I've tried making the cookie essential, and setting CheckConsentNeeded to false. This is my ConfigureServices method of the startup.cs file:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddMvc(option => option.EnableEndpointRouting = false);
            services.AddSession(options => { 
                options.Cookie.Name = "cart";
                options.Cookie.MaxAge = TimeSpan.FromDays(365);
                options.Cookie.IsEssential = true; // make the session cookie Essential
            });
            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["DefaultConnection"], b => b.MigrationsAssembly("ShopTutorial.Database")));

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
        }

app.UseSession(); has been added to the Configure method of the same class.


回答1:


I solved it by adding the cart information to a MemoryCache lasting for 60 seconds with the Stripe payment intent description (a new GUID) as a key:

        var intent = new CreatePaymentIntent();

        ObjectCache cache = MemoryCache.Default;

        cache.AddOrGetExisting(intent.Description, Cart, new CacheItemPolicy {
            AbsoluteExpiration =
                DateTimeOffset.Now.AddSeconds(60.0)
        });

Then, in the webhook I called the same cache with the specific key that is stored in the intent object's description sent back from Stripe:

        var charge = stripeEvent.Data.Object as Stripe.Charge;

        ObjectCache cache = MemoryCache.Default;

        var cart = (Cart)cache.Get(charge.Description);


来源:https://stackoverflow.com/questions/61080662/session-changed-in-webhook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!