使用SignInManager而不调用app.UseIdentity()

时间:2016-08-29 17:07:06

标签: c# asp.net asp.net-core asp.net-identity

我需要使用基本的SignIn机制登录用户到我的网站:

var result = await _signInManager.PasswordSignInAsync(username, password, false, lockoutOnFailure: false);

不幸的是它会抛出一个错误:

  

没有配置身份验证处理程序来处理该方案:Identity.Application

原因可能是:我没有在app.UseIdentity();中致电Startup.cs

我没有打电话,因为我想自己配置cookie。因此,我使用此代替UseIdentity

        app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = false,
            TokenValidationParameters = tokenValidationParameters
        });

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = false,
            AuthenticationScheme = "Cookie",
            CookieName = "access_token",
            TicketDataFormat = new CustomJwtDataFormat(
                SecurityAlgorithms.HmacSha256,
                tokenValidationParameters)
        });

如果我致电app.UseIdentity();PasswordSignInAsync正常工作,但CookieAuthentication的行为与UseIdentity中的配置相同(重定向到/Account/Login,此行为是在我的配置中禁用。)

正如我在source code中看到的那样,UseIdentity并没有比我更多(它只是使用UseCookieAuthentication,所以和我一样)。那么,为什么我的灵魂会引起问题?

如何使PasswordSignInAsync无法使用app.UseIdentity()工作?

1 个答案:

答案 0 :(得分:2)

它确实做了一些不同的事情,它使用IdentityOptions设置cookie中间件,并在验证用户时在signInManager中的其他地方使用相同的identityoptions。

具体来说,它使用“Identity.Application”作为应用程序cookie的auth方案的名称,因为它也会在尝试对用户进行身份验证时使用它,因为没有cookie中间件与authscheme匹配,因为用不同的方式命名你的。

如果您将AuthenticationScheme和CookieName命名为“Identity.Application”,那么它应该会超过该错误。

或者您可以配置IdentityOptions以匹配您选择的authenticationscheme和cookiename,以便在尝试对用户进行身份验证时从signinManager传入匹配值