2017-10-06 5 views
1

私の問題役割の請求名を設定する方法: [承認(役割=「L1」)]とUser.IsInRole(「L1」)が請求名を探している「http://schemas.microsoft.com/ws/2008/06/identity/claims/role」の代わりに具体的には、 "役割"IdentityServer4 /アイデンティティで

の: 私は、次のステップ(http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html)によって標準アイデンティティデータベースでIdentityServer4を作成しました:

  • は、新しいプロジェクト を作成します。
    • ASP.NETコアWebアプリケーション(.NETコア)
    • ASP.NETコア1.1
    • Webアプリケーション
    • 変更認証
      • 個々のユーザーが
  • アカウントIdentityServer4.AspNetIdentity + Config.cs +を追加...

次にMVCクライアントを作成しました。認証は正常に動作します。私は主張のリストも得ます。

IdentityServer4でロールを設定するために、テーブルAspNetUsers、AspNetUserRoles、およびAspNetRoles inを使用しています。ロールは、クレーム名「ロール」を使用してクレームに追加されます。

MVCクライアントでコントローラのアクションを承認しようとすると、自分の役割のクレーム名が間違っているようです。

どのように競合を解決できますか? 「ロール」を「http://schemas.microsoft.com/ws/2008/06/identity/claims/role」にマップする必要がありますか?ここで

はMVC-Clientで私のコントローラである:私はこの答え(https://stackoverflow.com/a/34226538/272357)を発見したが、私はCustomPrincipleを "登録" する方法がわからない

[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; 
    } 
} 

+1

一般的なClaimsPrinciple-Parameterを持つUserManagerがMicrosoft.AspNetCore.Identityに存在しないため、見つかった答えはasp.net-coreでは機能しません。 – Nikolaus

答えて

2

私は自分自身の答えを見つけました。 クレーム名の改名(コードを参照)を避けるため、私は JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); を使用していることを言及しておきます。

結果として、ClaimsPrincipleは「http://schemas.microsoft.com/ws/2008/06/identity/claims/role」という名前のロールを探していました。

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" 
     } 

    }); 
    } 
}