implementing roles in identity server 4 with asp.net identity

后端 未结 1 822
北荒
北荒 2021-02-02 17:48

I am working on an asp.net MVC application with identity server 4 as token service. I have an api as well which has some secure resources. I want to implement roles (Authorizati

相关标签:
1条回答
  • 2021-02-02 18:00

    First, you need to request "API" scope in your OpenIdConnectOptions().

    oidcOptions.Scope.Add("API");
    

    or

    Scope = { "API", "offline_access",..},
    

    Then you need to check if the role claim is included in the claims list available to your API controler(don't apply the roles filter in authorize attribute yet. Put a debug point inside controller method and expand User property). Check if the type of the role claim you received(listed in Claims Collection) matches User.Identity.RoleClaimType property

    If the role claim type you have and User.Identity.RoleClaimType doesn't match, authorize attribute with roles filter won't work. You can set the correct RoleClaimType in IdentityServerAuthenticationOptions() like follows

    app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
            {
                Authority = "http://localhost:5000",
                ScopeName = "API",
                RoleClaimType = ClaimTypes.Role,
                RequireHttpsMetadata = false
            });
    
    0 讨论(0)
提交回复
热议问题