【发布时间】:2017-02-22 11:47:42
【问题描述】:
该场景是我的客户可能获取有关我的用户的数据。有配置服务器的代码:
Startup.cs
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryIdentityResources(ApiConfiguration.GetIdentityResources()) .AddInMemoryApiResources(ApiConfiguration.GetAllResources())
.AddInMemoryClients(ApiConfiguration.GetAllClients())
.AddTestUsers(ApiConfiguration.GetUsers())
API配置
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 只能访问 特定 用户的令牌?
【问题讨论】:
标签: c# asp.net-core identityserver4