Webhook中的会话已更改 [英] Session Changed In Webhook

查看:54
本文介绍了Webhook中的会话已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Stripe和.Net建立一个网上商店,但是我遇到了一些问题,即会话无法持久保存在我的Webhook中,因此该Webhook无法获取购物车或客户信息.

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);

如果我查看_session,我可以看到它有一个ID.但是,当我尝试从Webhook获取购物车时,会话ID已更改,因此购物车为空.

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.

我的get cart方法会自动触发.使用网络挂钩成功,并开始如下操作:

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

我尝试使cookie成为必需,并将CheckConsentNeeded设置为false.这是我的startup.cs文件的ConfigureServices方法:

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();已添加到同一类的Configure方法中.

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

推荐答案

我通过将购物车信息添加到持续60秒的MemoryCache中来解决了该问题以"Stripe付款意图说明"(新的GUID)为键:

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)
        });

然后,在webhook中,我使用从Stripe发回的intent对象的描述中存储的特定键调用了相同的缓存:

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);

这篇关于Webhook中的会话已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆