该方案是我的客户可以获取有关我的用户的数据。有配置服务器的代码:
Startup.cs
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryIdentityResources(ApiConfiguration.GetIdentityResources()) .AddInMemoryApiResources(ApiConfiguration.GetAllResources())
.AddInMemoryClients(ApiConfiguration.GetAllClients())
.AddTestUsers(ApiConfiguration.GetUsers())
ApiConfiguration
public static IEnumerable<ApiResource> GetAllResources()
{
yield return new ApiResource
{
Name = "customAPI",
Description = "Custom API Access",
UserClaims = new List<string> { "role" },
ApiSecrets = new List<Secret> { new Secret("scopeSecret".Sha256()) },
Scopes = new List<Scope> {
new Scope("customAPI"),
}
};
}
public static IEnumerable<Client> GetAllClients()
{
yield return new Client
{
ClientId = "oauthClient",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets = new List<Secret> {
new Secret("Password".Sha256())},
AllowedScopes = new List<string> { "customAPI" }
};
}
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
}
public static List<TestUser> GetUsers()
{
return new List<TestUser>
{
new TestUser
{
SubjectId = "2",
Username = "bob",
Password = "psw",
Claims = new List<Claim> {
new Claim(ClaimTypes.Email, "1@1.com"),
new Claim(ClaimTypes.Role, "admin")
}
}
};
}
有了这个请求:
POST /connect/token
Headers:
Content-Type: application/x-www-form-urlencoded
Body:
grant_type=client_credentials&scope=customAPI&client_id=oauthClient&client_secret=Password
客户端获得了访问令牌。所以我的问题是如何使用令牌?我需要http-request来获取有关bob(测试用户)的数据?
还有另一个相关问题:如何为我的客户端配置api只能访问特定用户的令牌?
答案 0 :(得分:1)
查看上面的配置,您设置的客户端正在使用ClientCredentials授权,这是OAuth授权类型。如果您希望添加标识,则应该使用OpenID Connect授权类型,这将为您提供特定于您进行身份验证的用户的标识和标记。
有关使用OpenID Connect与Identity Server的更多信息,请参阅http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html的文档
答案 1 :(得分:0)
如果您完成以下快速入门教程,您将看到它汇集在一起。
(您可以跳过外部认证上的那个。)
https://identityserver4.readthedocs.io/en/release/quickstarts/0_overview.html