亲宝软件园·资讯

展开

ASP.NET Core使用Cookie

Sweet-Tang 人气:0

ASP.NET Core 1.x提供了通过Cookie 中间件将用户主体序列化为一个加密的Cookie,然后在后续请求中验证Cookie并重新创建主体,并将其分配给HttpContext.User属性。如果您要提供自己的登录界面和用户数据库,可以使用作为独立功能的Cookie中间件。

ASP.NET Core 2.x的一个主要变化是不再存在Cookie中间件。取而代之的是在Startup.cs文件中的Configure方法中的调用UseAuthentication方法会添加设置HttpContext.User属性的 AuthenticationMiddleware 中间件。

添加配置

ASP.NET Core 1.x

按下列步骤操作:

        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AccessDeniedPath = "/Account/Forbidden/",
            AuthenticationScheme = "MyCookieAuthenticationScheme",
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            LoginPath = "/Account/Unauthorized/"
        });

ASP.NET Core 2.x

按下列步骤操作:

        app.UseAuthentication();
        services.AddAuthentication("MyCookieAuthenticationScheme")
                .AddCookie("MyCookieAuthenticationScheme", options => {
                    options.AccessDeniedPath = "/Account/Forbidden/";
                    options.LoginPath = "/Account/Unauthorized/";
                });

上面的代码片段配置了以下部分或全部选项:

其它选项包括为Cookie认证创建的设置选项,身份验证的Cookie的名称,Cookie的域和Cookie各种安全属性。默认情况下,Cookie身份验证为其创建的任何Cookie使用适当的安全选项,例如:

创建身份认证Cookie

要创建一个保存用户信息的cookie,您必须构建一个ClaimsPrincipal 保存您希望序列化到Cookie中的信息。

ASP.NET Core 1.x

    await HttpContext.Authentication.SignInAsync("MyCookieAuthenticationScheme", principal);

ASP.NET Core 2.x

    await HttpContext.SignInAsync("MyCookieAuthenticationScheme", principal);

这将创建一个加密的Cookie并将其添加到当前响应中。在调用SignInAsync时,必须在配置中指定的AuthenticationScheme

顺便提一下,使用的加密方式是ASP.NET Core的Data Protection系统。如果您在多台机器上进行托管、负载平衡或使用Web集群,则需要配置Data Protection才能使用相同的密钥和应用程序标识符。

Signing out(登出)

要退出当前用户并删除其Cookie,请在控制器中调用以下方法:

ASP.NET Core 1.x

    await HttpContext.Authentication.SignOutAsync("MyCookieAuthenticationScheme");

ASP.NET Core 2.x

    await HttpContext.SignOutAsync("MyCookieAuthenticationScheme");

服务端变化反馈

警告: 一旦创建了认证的Cookie,它将成为唯一的身份来源。即使您在服务系统中禁用用户,Cookie身份验证也无法了解此信息,只要Cookie有效,用户仍可登录。

Cookie认证在其选项中提供了一系列事件。ValidateAsync()事件可用于拦截和重写Cookie身份验证。

可以考虑在后端用户数据库中增加LastChanged列。为了在数据库更改时使Cookie无效,您应该首先在创建Cookie时添加一个LastChanged包含当前值的声明。数据库更改时,更新LastChanged例的值。

要重写ValidateAsync()事件的实现,您必须编写一个具有以下签名的方法:

    Task ValidateAsync(CookieValidatePrincipalContext context);

ASP.NET Core Identity 在SecurityStampValidator实现了这一逻辑,链接地址。示例如下所示:

ASP.NET Core 1.x

    public static class LastChangedValidator
    {
        public static async Task ValidateAsync(CookieValidatePrincipalContext context)
        {
            // Pull database from registered DI services.
            var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
            var userPrincipal = context.Principal;
    
            // Look for the last changed claim.
            string lastChanged;
            lastChanged = (from c in userPrincipal.Claims
                            where c.Type == "LastUpdated"
                            select c.Value).FirstOrDefault();
    
            if (string.IsNullOrEmpty(lastChanged) ||
                !userRepository.ValidateLastChanged(userPrincipal, lastChanged))
            {
                context.RejectPrincipal();
                await context.HttpContext.Authentication.SignOutAsync("MyCookieAuthenticationScheme");
            }
        }
    }

然后,在Startup.cs文件中的Configure方法中将Cokie认证配置进行重写:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        Events = new CookieAuthenticationEvents
        {
            OnValidatePrincipal = LastChangedValidator.ValidateAsync
        }
    });

ASP.NET Core 2.x

    public static class LastChangedValidator
    {
        public static async Task ValidateAsync(CookieValidatePrincipalContext context)
        {
            // Pull database from registered DI services.
            var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
            var userPrincipal = context.Principal;
    
            // Look for the last changed claim.
            string lastChanged;
            lastChanged = (from c in userPrincipal.Claims
                            where c.Type == "LastUpdated"
                            select c.Value).FirstOrDefault();
    
            if (string.IsNullOrEmpty(lastChanged) ||
                !userRepository.ValidateLastChanged(userPrincipal, lastChanged))
            {
                context.RejectPrincipal();
                await context.HttpContext.SignOutAsync("MyCookieAuthenticationScheme");
            }
        }
    }

然后,将在Startup.cs的ConfigureServices方法中将Cookie服务注册进行配置:

    services.AddAuthentication("MyCookieAuthenticationScheme")
            .AddCookie(options =>
            {
                options.Events = new CookieAuthenticationEvents
                {
                    OnValidatePrincipal = LastChangedValidator.ValidateAsync
                };
            });

如果要非破坏性地更新用户主体,可以调用context.ReplacePrincipal(),并将context.ShouldRenew属性设置为true

Cookie设置选项

CookieAuthenticationOptions类提供了各种配置选项,在创建时调整Cookie的配置。

ASP.NET Core 1.x

在Startup.cs文件中的Configure方法中使用CookieAuthenticationOptions的例子如下:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        CookieName = "AuthCookie",
        CookieDomain = "contoso.com",
        CookiePath = "/",
        CookieHttpOnly = true,
        CookieSecure = CookieSecurePolicy.Always
    });

ASP.NET Core 2.x

ASP.NET Core 2.x 统一了用于配置Cookie的API。1.x API已被标记为过时,并且在CookieAuthenticationOptions类中引入了一种类型为CookieBuilder新的Cookie属性。建议您迁移到2.x API。

在Startup.cs的ConfigureServices方法中使用CookieAuthenticationOptions的例子如下:

    services.AddAuthentication()
        .AddCookie(options =>
        {
            options.Cookie.Name = "AuthCookie";
            options.Cookie.Domain = "contoso.com";
            options.Cookie.Path = "/";
            options.Cookie.HttpOnly = true;
            options.Cookie.SameSite = SameSiteMode.Lax;
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        });

持久Cookie

您可能希望Cookie在浏览器会话中持续存在,并希望设置身份和Cookie传输的绝对过期时间。这种持久性应该只能是用户显示同意,在登录时的“记住我”复选框或类似的机制启用。您可以通过在创建身份认证Cookie时调用的SignInAsync方法中使用AuthenticationProperties参数来执行这些操作。例如:

ASP.NET Core 1.x

    await HttpContext.Authentication.SignInAsync(
        "MyCookieAuthenticationScheme",
        principal,
        new AuthenticationProperties
        {
            IsPersistent = true
        });

上述代码片段中使用的AuthenticationProperties类,位于Microsoft.AspNetCore.Http.Authentication命名空间中。

ASP.NET Core 2.x

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

上述代码片段中使用的AuthenticationProperties类,位于Microsoft.AspNetCore.Authentication命名空间中。

上面的代码段创建一个身份和相应的Cookie,直到浏览器关闭。以前通过Cookie设置选项配置的任何滑动过期设置仍然有效。如果Cookie在浏览器关闭时过期,浏览器会在重新启动后清除它。如果Cookie在浏览器关闭时过期,浏览器会在重新启动后清除它。

绝对到期时间

ASP.NET Core 1.x

    await HttpContext.Authentication.SignInAsync(
        "MyCookieAuthenticationScheme",
        principal,
        new AuthenticationProperties
        {
            ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
        });

ASP.NET Core 2.x

    await HttpContext.SignInAsync(
        "MyCookieAuthenticationScheme",
        principal,
        new AuthenticationProperties
        {
            ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
        });

上述代码段创建一个持续20分钟的身份和相应的cookie。这将忽略以前通过Cookie设置选项配置的任何滑动过期设置。

ExpiresUtcIsPersistent属性是互斥的。

到此这篇关于ASP.NET Core中Cookie验证身份用法详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论