Identity Server 4 を使い始めたばかりです。
Client Credentials 付与タイプを使用して API を保護しようとしています。
IS4 内に API をセットアップしています。
public static IEnumerable<ApiResource> Apis =>
new List<ApiResource>
{
new ApiResource("myapi", "Test API")
{
ApiSecrets = { new Secret("secret".Sha256() )}
}
};
次のクライアント設定もあります。
public static IEnumerable<Client> Clients =>
new List<Client>
{
new Client
{
ClientId = "testc",
ClientName= "Test Client",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("m2msecret".Sha256())
},
AllowedScopes = new List<string>
{
"myapi"
}
},
};
API 内に保護したいコントローラーがあります。
[Authorize]
public class TestController : ControllerBase {}
次に、次のようにトークン リクエストを作成するとします。
var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = disco.TokenEndpoint,
ClientId = "testc",
ClientSecret = "m2msecret",
Scope = "myapi",
});
これにより、API を呼び出してリソースにアクセスできるようになります。完全!
しかし、コントローラーを役割で保護したい、例えば
[Authorize(Roles = "admin")]
public class TestController : ControllerBase {}
だから私の2つの質問は次のとおりです。
1: クライアント資格情報を使用してロールベースの承認を設定するにはどうすればよいですか?
2: クライアント資格情報はユーザーにリンクされていないため、記録の変更の監査証跡を保持するにはどうすればよいですか (たとえば、サプライヤー X が userId 5 によって更新されたなど)。
ありがとう