[英]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.