Asp Core 2.1 Jwt + Identity. userManager store does not implement IUserRoleStore

前端 未结 1 1254
北海茫月
北海茫月 2021-01-22 10:58

I am trying to work with Jwt auth and Identity in ASP Net Core 2.1

In my Startup.cs I have:

services.AddAuthentication(JwtBearerDefaults.AuthenticationSc         


        
相关标签:
1条回答
  • 2021-01-22 11:43

    When you use AddIdentity<TUser, TRole>, that call configures the default authentication scheme, like so (source):

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
        options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
        options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    })
    

    In your Startup.ConfigureServices, you have the following, which also sets the default authentication scheme:

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    

    Because of the order this is defined (AddIdentity is after AddAuthentication), the default is changing from Jwt to Identity, so that when you use [Authorize], the authentication process is now expecting to use Identity rather than Jwt.

    To resolve this, the simplest option is to switch the order of AddIdentity and AddAuthentication, so the JwtBearer call comes last and therefore "wins". You'll also need to be more explicit and set both DefaultAuthenticateScheme and DefaultChallengeScheme:

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(...);
    

    Another option is to be explicit in the [Authorize] attribute, calling out which authentication scheme you want to use, like either of the following two lines:

    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [Authorize(AuthenticationSchemes = IdentityConstants.ApplicationScheme)]
    

    It seems the first option would be most appropriate for your use-case, but it's good to know that this second option exists should you need it as you go further with Identity (there are more - e.g. using policies).

    0 讨论(0)
提交回复
热议问题