如何在IdentityServer4 / Identity

时间:2017-10-06 08:56:30

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

我的问题: [授权(Roles =“L1”)]和User.IsInRole(“L1”)正在寻找声明名称“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”而不是“角色”

具体: 我通过以下步骤(http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html)创建了一个带有标准身份数据库的IdentityServer4:

  • 创建新项目
    • ASP.NET核心Web应用程序(.NET核心)
    • ASP.NET Core 1.1
    • 网络应用程序
    • 更改身份验证
      • 个人用户帐户
  • 添加IdentityServer4.AspNetIdentity + Config.cs + ...

然后我创建了一个MVC-Client。身份验证工作正常。我还得到了一份索赔清单。

我正在使用表AspNetUsers,AspNetUserRoles和AspNetRoles来为IdentityServer4配置角色。这些角色将添加到声明名称为“role”的声明中。

如果我尝试在MVC-Client中授权我的控制器操作,那么我的角色的声明名称似乎是错误的。

我该如何解决冲突?我是否必须将“角色”映射到“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”?

这是我在MVC-Client中的控制器:

[Route("api/[controller]")]
public class CompaniesController : Controller
{
    [HttpGet]
    //[Authorize(Roles = "L1")] // This looks for the claim http://schemas.microsoft.com/ws/2008/06/identity/claims/role instead of role
    public async Task<IEnumerable<Company>> GetCompaniesAsync()
    {
        var c = User.Identities.Count(); // 1
        var nameOfExptectedRoleClaimType = User.Identities.First().RoleClaimType; // http://schemas.microsoft.com/ws/2008/06/identity/claims/role
        var b0 = User.HasClaim(nameOfExptectedRoleClaimType, "L1"); // false
        var b1 = User.HasClaim("role", "L1"); // true
        var b2 = User.IsInRole("L1"); // false; looks for claim http://schemas.microsoft.com/ws/2008/06/identity/claims/role; used by [Authorize(Roles = "L1")]

        var companies = await _crmApi.GetCompaniesAsync();

        return companies;
    }
}

我找到了这个答案(https://stackoverflow.com/a/34226538/272357),但我不知道如何“注册”CustomPrinciple。

1 个答案:

答案 0 :(得分:2)

我找到了自己的答案。 我必须提到我正在使用 JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 避免重命名声明名称(请参阅代码)。

由此产生的问题是ClaimsPrinciple仍然会查找以&#34; http://schemas.microsoft.com/ws/2008/06/identity/claims/role&#34;命名的角色。

这可以通过OpenIdConnectOptions.TokenValidationParameters间接修复。

public class Startup
{
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  {
    // ...

    // Avoid claim mapping to old ms soap namespaces. Avoid replace "role" by "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
    {
      // ...

      // https://leastprivilege.com/2016/08/21/why-does-my-authorize-attribute-not-work/
      TokenValidationParameters = new TokenValidationParameters
      {
        NameClaimType = "name",
        RoleClaimType = "role", // The role claim type is named "role" instead of "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
      }

    });
  }
}
相关问题