[英]Identity Server Refresh Token Resource Owner Password Credential Flow
[英]Getting claims in identity server using resource owner password
我正在使用身份服务器4进行身份验证,使用授予类型为“ ResourceOwnerPassword”。 我能够验证用户身份,但无法获得与用户相关的声明。 那我怎样才能得到那些呢?
下面是我的代码
客户
Startup.cs
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ApiName = "api1"
});
控制者
public async Task<IActionResult> Authentication(LoginViewModel model)
{
var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");
var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync(model.Email, model.Password, "api1");
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
}
// Here I am not getting the claims, it is coming Forbidden
var extraClaims = new UserInfoClient(disco.UserInfoEndpoint);
var identityClaims = await extraClaims.GetAsync(tokenResponse.AccessToken);
if (!tokenResponse.IsError)
{
Console.WriteLine(identityClaims.Json);
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
}
服务器启动
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients(Configuration))
.AddAspNetIdentity<ApplicationUser>()
.AddProfileService<IdentityProfileService>()
.AddResourceOwnerValidator<ResourceOwnerPasswordValidator>();
Config.cs
public static IEnumerable<Client> GetClients(IConfigurationRoot Configuration)
{
// client credentials client
return new List<Client>
{
// resource owner password grant client
new Client
{
ClientId = "ro.client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AlwaysSendClientClaims = true,
AlwaysIncludeUserClaimsInIdToken = true,
AccessTokenType = AccessTokenType.Jwt
}
};
}
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api1", "My API")
};
}
但是,当我在jwt.io中检查访问令牌时,我可以看到声明。但是为什么我不能进入控制器?
任何帮助对此表示赞赏!
您可以按照示例调用UserInfoEndpoint
,但是如果将ApiResource
定义为要求它们,那么您还可以获得其他声明。
例如,不仅仅是像您这样定义ApiResource
:
new ApiResource("api1", "My API")
您可以使用扩展格式并定义获取该作用域的访问令牌时想要的UserClaims
。 例如:
new ApiResource
{
Name = "api1",
ApiSecrets = { new Secret(*some secret*) },
UserClaims = {
JwtClaimTypes.Email,
JwtClaimTypes.PhoneNumber,
JwtClaimTypes.GivenName,
JwtClaimTypes.FamilyName,
JwtClaimTypes.PreferredUserName
},
Description = "My API",
DisplayName = "MyApi1",
Enabled = true,
Scopes = { new Scope("api1") }
}
然后,在您自己的IProfileService
实现中,您将发现对GetProfileDataAsync
调用具有上下文中请求的声明的列表( ProfileDataRequestContext.RequestedClaimTypes
)。 给出要求的清单后,您就可以将自己喜欢的任何声明添加到context.IssuedClaims
中。从该方法返回的context.IssuedClaims
。 这些将成为访问令牌的一部分。
但是,如果仅通过专门调用UserInfo端点仅希望某些声明,则需要创建IdentityResource
定义,并将该范围包含在原始令牌请求中。 例如:
new IdentityResource
{
Name = "MyIdentityScope",
UserClaims = {
JwtClaimTypes.EmailVerified,
JwtClaimTypes.PhoneNumberVerified
}
}
但是,您的第一个问题是在此处遵循其他答案,因此您不会因为对UserInfo端点的响应而被“禁止”!
调用UserInfoEndpoint时,尝试沿着请求发送令牌。 尝试这个:
var userInfoClient = new UserInfoClient(doc.UserInfoEndpoint, token);
var response = await userInfoClient.GetAsync();
var claims = response.Claims;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.