2

IdentityServer4.EntityFramework 1.0.0パッケージを使用して、SQL Serverに格納されているクライアントおよびAPIリソースの構成情報とともにIdentityServer4 1.0.0を使用しています。IdentityServer4 APIリソースの必須クレームを設定するにはどうすればよいですか?

IdentityServer4.EntityFramework 1.0.0パッケージで管理されているテーブルでAPIにアクセスするために必要なクレームのリストを設定する方法はありますか?

+0

「https:// localhost:33132/identity /。well-known/openid-configuration」から設定を取得することができません。このエラーが発生するため、IDサーバ4で証明書を使用する必要がありますか。 –

答えて

0
あなたのAPIのスタートアップで

services.AddAuthorization(options => 
     { 
      options.AddPolicy("SecurePolicyName", policyUser => 
      { 
       policyUser.RequireClaim("role", "admin"); 
      }); 
     }); 

その後、あなたのコントローラの使用にAuhtorizeは、あなたのポリシーに属性。

[Authorize(Policy = "SecurePolicyName")] 
+0

ありがとうございます。私は同じことをする方法を探していますが、既存のIdentityServer4オブジェクトモデルを利用しています。 – aaronR

0

私は同じ問題を抱えていた、奇妙なことは、私が追加したクレームは、アクセストークンに存在したが、セキュリティで保護されたAPIはそれを拒否しました。あなたのstartup.csで

[Produces("application/json")] 
[Authorize("Founder")] 
[Route("api/ApiResourceWithPolicy")] 
public class ApiResourceWithPolicyController : Controller 
{ 
    [HttpGet] 
    public IActionResult Get() 
    { 
     return new JsonResult(new { ResourceType = "With Policy", ResourceName = "Api2" }); 
    } 
} 

そして、このポリシー設定:

ervices.AddAuthorization(options => options.AddPolicy("Founder", policy => policy.RequireClaim("Employee", "Mosalla"))); 

今すぐに必要な主張を追加する何をする必要があると私たちは、このAPIがあると、あなたのクライアントにクレームを追加することですあなたのクライアントは、ここで重要な点はClientClaimsPrefixに空の文字列を設定することです:IdentityServer4は主張をプレフィックスとリソースサーバにそれらを認識できない作るためだ

public static IEnumerable<Client> GetClients() 
    { 
     return new List<Client> 
     { 
      new Client 
      { 
       ClientId = "client1", 

       // no interactive user, use the clientid/secret for authentication 
       AllowedGrantTypes = GrantTypes.ClientCredentials, 

       // secret for authentication 
       ClientSecrets = 
       { 
        new Secret("123654".Sha256()) 
       }, 

       // scopes that client has access to 
       AllowedScopes = {"Api1"}, 
       Claims = new[] 
       { 
        new Claim("Employee", "Mosalla"), 
        new Claim("website", "http://hamidmosalla.com") 
       }, 
       ClientClaimsPrefix = "" 
      } 
    } 

enter image description here

あなたはより多くの説明訪問my post about policy-based authorization with IdentityServer4が必要な場合。

関連する問題