对于基于 cookie 的身份验证,这两种 cookie 配置有什么区别?

zer*_*roG 3 authentication cookies identity asp.net-identity asp.net-core

对于基于 cookie 的身份验证,这两种 cookie 配置有什么区别?

变体1

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    // Configure cookie based authentication:
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(opt =>
            {
                /* validation rules */    
            });
}
Run Code Online (Sandbox Code Playgroud)

变体2

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<AppUser, AppRole>(opt =>
    {
        /* validation rules */
    });

    services.ConfigureApplicationCookie(options =>
    {
        options.LoginPath = new PathString("/User/Login");

        options.Cookie = new CookieBuilder
        {
            Name = "AspNetCoreIdentityExampleCookie",
            HttpOnly = false,
            SameSite = SameSiteMode.Lax,
            SecurePolicy = CookieSecurePolicy.Always
        };

        options.ExpireTimeSpan = TimeSpan.FromMinutes(2);
        options.SlidingExpiration = true;
    });
}
Run Code Online (Sandbox Code Playgroud)

我无法理解其中的区别,我会很高兴听到任何帮助。

小智 5

变体一涉及手动配置基于 cookie 的身份验证,而不使用提供的身份框架。另一方面,变体二涉及调用AddIdentity,它会自动添加基于 cookie 的身份验证并允许使用 自定义配置ConfigureApplicationCookie

参考。