【问题标题】:Asp.net Core Persistent Authentication - Custom Cookie AuthenticationAsp.net Core 持久身份验证 - 自定义 Cookie 身份验证
【发布时间】:2018-02-24 21:21:03
【问题描述】:

我正在尝试建立持久连接,因此用户只需使用一次密码。我使用过这个文档:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x,但用户在一段时间后仍然会断开连接。

await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    principal,
                    new AuthenticationProperties
                    {
                        IsPersistent = true
                    });

如何才能获得真正持久的连接?

【问题讨论】:

    标签: asp.net-core asp.net-core-2.0


    【解决方案1】:

    根据文档,IsPersistent 授予的持久性仅意味着身份验证将通过浏览会话持续存在(即,即使在浏览器关闭时也会保留)。您需要结合使用持久性 来设置 cookie 的过期时间。 cookie 的过期时间可以通过CookieAuthenticationOptions (MSDN) 使用 ExpireTimeSpan 选项设置。

    没有持久性,可以使用AuthenticationOptions中的ExpiresUtc选项设置身份验证过期,

    【讨论】:

    • 好吧,我试试。文档说只有一件事:ExpiresUtc 和 IsPersistent 属性是互斥的。这是否意味着我不能同时使用两者?
    • @GlobalFx 因此,即使用户没有关闭浏览器,绝对到期(通过 ExpiresUtc)也会拒绝设置的 cookie。 IsPersistent 表示用户的身份验证生命周期由 CookieAuthenticationOptions 决定。将 isPersistent 设置为 false 意味着用户只针对他们当前的浏览会话进行身份验证,当浏览器关闭时会被清除。
    • 好的,所以解决方案是将 IsPersistent 设置为 true 并将 ExpireTimeSpan 设置为假设 ConfigureServices中的一年> 不设置 ExpiresUtc。用户每年只需要登录一次吗?
    • 我相信!无论您的应用程序使用什么 cookie。如果您没有设置持久性,则在关闭浏览器时会清除 cookie。如果您确实有持久性,那么 cookie 需要一个到期日期,由您的 TimeSpan 设置。
    • 太棒了!想要更新您的答案以便我接受吗?
    【解决方案2】:

    在实现持久性 cookie 身份验证时,您应该注意几件事。

    在 Startup.cs 中为 cookie 配置滑动过期。如果您明确设置所需的值并且不使用默认设置,则会更清楚。

    private void ConfigureAuthentication(IServiceCollection services)
    {
        services
            .AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {                 
                // true by default   
                options.SlidingExpiration = true;
    
                // 14 days by default
                options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
            });
    }
    

    当用户选中“记住我”标志时,配置 cookie 以在浏览器会话中持续存在并设置绝对过期时间(只要您愿意)。此设置将覆盖 SlidingExpiration 和 ExpireTimeSpan。在登录操作中:

    List<Claim> claims = new List<Claim>();
    // Prepare user claims...
                    
    var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
    
    AuthenticationProperties authenticationProperties = new AuthenticationProperties() { IsPersistent = model.RememberMe };
    if (model.RememberMe)
    {
        // One month for example
        authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMonths(1);
    }
    
    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, authenticationProperties);
    

    配置数据保护。记住旧经典 asp.net 网络表单中的 machineKey。否则,每次 IIS 应用程序池重新启动后,cookie 都会被重置。您应该在 Startup.cs 中进行身份验证之前配置数据保护。将密钥存储在应用的根文件夹中:

    private void ConfigureDataProtection(IServiceCollection services, IWebHostEnvironment environment)
    {
        var keysDirectoryName = "Keys";
        var keysDirectoryPath = Path.Combine(environment.ContentRootPath, keysDirectoryName);
        if (!Directory.Exists(keysDirectoryPath))
        {
            Directory.CreateDirectory(keysDirectoryPath);
        }
        services.AddDataProtection()
              .PersistKeysToFileSystem(new DirectoryInfo(keysDirectoryPath))
              .SetApplicationName("YourApplicationName");
    }
    

    来自文档:

    【讨论】:

    • 这应该标记为已接受的答案
    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 2017-03-30
    • 2012-08-01
    • 2017-10-26
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多