如何在.NET Core 3.1中使用CookieAuthentication获取cookie续订信息?

huangapple go评论71阅读模式
英文:

How to get cookie renewal information in .NET Core 3.1 with CookieAuthentication?

问题

options.Events = new CookieAuthenticationEvents
{
    OnValidatePrincipal = context =>
    {
        if (context.ShouldRenew)
        {
            Console.WriteLine("Cookie expire time should be updated");
            // Write your desired logic here
        }

        return Task.CompletedTask;
    }
};
英文:

I am using .NET Core 3.1 and would like to know how to get cookie renewal information when a user's login cookie expire time is extended.

I have tried adding the following code to startup.cs, but context.ShouldRenew always returns false, even though the login cookie's expire time has been extended:


            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = new PathString("/Member/Login");
                    options.SlidingExpiration = true;
                    options.ExpireTimeSpan = TimeSpan.FromSeconds(10);
                    options.Events = new CookieAuthenticationEvents
                    {
                        OnValidatePrincipal = context =>
                        {
                            if (context.ShouldRenew)
                            {
                                Console.WriteLine("Cookie expire time should be update");
                                // I want to write some text
                                // But `context.ShouldRenew` always get false
                            }

                            return Task.CompletedTask;
                        }
                    };
                });

What should be the correct way to get cookie renewal information in .NET Core 3.1 with CookieAuthentication?

答案1

得分: 1

我认为你需要使用另一个属性来检测 cookie 是否过期。该属性的描述 ShouldRenew

> 如果为 true,cookie 将被续订

换句话说:即使 cookie 已过期,它也可以为 false。

你可以使用 HttpContext 属性来检查是否有任何 cookie 过期:

options.Events = new CookieAuthenticationEvents
{
    OnValidatePrincipal = context =>
    {
        var cookie = context.HttpContext.Request.Cookies["YOUR_COOKIE_NAME"];

        if (cookie.Expires < DateTime.Now)
        {
            // 在这里编写你的代码...
        }

        return Task.CompletedTask;
    }
};
英文:

I think you need to use another property to detect if cookie has expired. Description of the property ShouldRenew:

> If true, the cookie will be renewed

In other words: It can be false, even if cookies have expired.

You can use HttpContext property to check if any cookies have expired:

options.Events = new CookieAuthenticationEvents
{
    OnValidatePrincipal = context =&gt;
    {
        var cookie = context.HttpContext.Request.Cookies[&quot;YOUR_COOKIE_NAME&quot;];

        if (cookie.Expires &lt; DateTime.Now)
        {
            // Your code here...
        }

        return Task.CompletedTask;
    }
};

huangapple
  • 本文由 发表于 2023年4月17日 21:40:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定