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

时间:2017-10-18 07:51:28

标签: c# asp.net-mvc cookies asp.net-core asp.net-core-2.0

我按照Microsoft的这篇文章(https://docs.microsoft.com/en-us/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,localhost over https,Chrome 61。

4 个答案:

答案 0 :(得分:1)

我认为您应该使用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

答案 1 :(得分:0)

假设您在localhost上提供服务,似乎Chrome browser未设置IP或内联网主机名(如localhost)的Cookie。您可以从IIS提供应用程序,并使用具有有效主机名的绑定。

答案 2 :(得分:0)

在尝试了许多谷歌搜索和stackoverflow答案几个小时后,尽管从SignInManager获得了成功,但我还是无法登录,但我遇到了同样的问题。 所以我添加了解决问题的方法 .AspNetCore.Identity.Application在Cookie名称中手动设置,并在value字段中添加了一个随机令牌,然后我就能够成功登录。注销后,身份令牌再次从cookie中消失了,但是再次登录后,这次它进入了cookie中。

我希望这一过程对像我这样经历过的人有所帮助。

答案 3 :(得分:-1)

升级我们网站的.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
            }));
}

希望这有助于某人!