ASP.NET Core 2.0身份验证Cookie未设置

我遵循Microsoft的这篇文章(https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x)来迁移我的.NET Core 2.0 MVC应用程序中的身份验证过程。

Startup.cs(ConfigureServices)

services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

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

Startup.cs(配置)

app.UseAuthentication();

AccountController.cs

List<Claim> claims = new List<Claim> {
                        new Claim(ClaimTypes.Name, "testUser"),
                        new Claim(ClaimTypes.Email, model.Email),
                        //new Claim("ID", user.ID.ToString(), ClaimValueTypes.Integer),
                        new Claim(ClaimTypes.Role, "Admin")
                    };

ClaimsIdentity identity = new ClaimsIdentity(claims, "MyCookieAuthenticationScheme");

ClaimsPrincipal principal = new ClaimsPrincipal(identity);

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

不幸的是我的.NET Cookie从未设置。 这意味着User.Identity.IsAuthenticated始终是false。 我尝试了许多Cookie选项,例如将Cookie.SameSite或Cookie.SecurePolicy更改为所有可能的值。

我使用Visual Studio 2017,通过https访问本地主机,Chrome 61。


我认为你应该使用Identity的UserManager类而不是HttpContext.SignInAsync来提供登录过程。 将IUs​​erManager注入到您的控制器构造函数中,并使用它来登录。

AccountController: Controller
{
    private readonly SignInManager<ApplicationUser> _signInManager;

    public AccountController(SignInManager<ApplicationUser> singInManager)
    {
        _signInManager = signInManager;
    }

    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
        ...
    }

}

您可以在Startup.cs中修改Identity的cookie设置。 看一眼:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity


假设您在localhost上为您的应用程序提供服务,似乎Chrome浏览器不会为IP或Intranet主机名(如localhost设置cookie。 您可以从IIS提供应用程序,并使用具有有效主机名的绑定。


当升级我们网站的.NET Core 2.0认证系统时,我不得不更新我们的控制器方法,以使用AuthenticationHttpContextExtensions.SignInAsync()方法而不是旧的HttpContext.SignInAsync()

例:

public async Task ClaimsLogin() {

    // Claims identity creation here...

    ClaimsPrincipal principal = new ClaimsPrincipal(identity);

    await Task.FromResult(
        AuthenticationHttpContextExtensions.SignInAsync(
            this.httpContextAccessor.HttpContext,
            "NameOfYourCookieHere",
            userPrincipal,
            new AuthenticationProperties()
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(2880),
                IsPersistent = false,
                AllowRefresh = false
            }));
}

希望这可以帮助别人!

链接地址: http://www.djcxy.com/p/90191.html

上一篇: ASP.NET Core 2.0 Authentication Cookie not set

下一篇: Role based Authorization with Identity in .Net Core 1.1