のカスタムスコープを設定する私はかなりのOpenIDへの新規およびMVCでのauth午前と認証コードフローサンプルに示すようにOpenIddict認証サーバとクライアントMVCアプリケーションを作成しています。 https://github.com/openiddict/openiddict-samples アプリケーションのユーザーからクレームにcompanyidを追加する必要があり、必要な手順を見つけようとしています。OpenIddict MVCコア2.0
は、これまでのところ私は、次を発見:
私が新しく追加された請求の範囲と新しいclaimspricipalを作成し、ticket.SetScopesに呼び出すためSCOPを追加することにより、AuthorizationController.CreateTicketAsyncでチケットに、特許請求の範囲および範囲を追加します。
// Add my specific claims: CompanyId and CompanyName
var claims = new List<Claim>();
claims.Add(new Claim(MyClaimsConstants.Claims.CompanyId, user.SelectedCompanyId.ToString()));
T_Company company = await _companyRepo.GetCompany(user.SelectedCompanyId);
claims.Add(new Claim(MyClaimsConstants.Claims.CompanyName, company.CompanyName));
// Create the new identity with the added claims
var newIdentity = new ClaimsIdentity(principal.Identity, claims);
principal = new ClaimsPrincipal(newIdentity);
....
if (!request.IsAuthorizationCodeGrantType())
{
// Set the list of scopes granted to the client application.
// Note: the offline_access scope must be granted
// to allow OpenIddict to return a refresh token.
ticket.SetScopes(new[]
{
OpenIdConnectConstants.Scopes.OpenId,
OpenIdConnectConstants.Scopes.Email,
OpenIdConnectConstants.Scopes.Profile,
OpenIdConnectConstants.Scopes.OfflineAccess,
OpenIddictConstants.Scopes.Roles,
MyClaimsConstants.Scopes.Company // <-
}.Intersect(request.GetScopes()));
}
私はIdentityTokenに追加するAuthrizationController.CreateTicketAsyncの私の主張に送信先を追加します。
foreach (var claim in ticket.Principal.Claims)
{
// Never include the security stamp in the access and identity tokens, as it's a secret value.
if (claim.Type == _identityOptions.Value.ClaimsIdentity.SecurityStampClaimType)
{
continue;
}
var destinations = new List<string>
{
OpenIdConnectConstants.Destinations.AccessToken
};
// Only add the iterated claim to the id_token if the corresponding scope was granted to the client application.
// The other claims will only be added to the access_token, which is encrypted when using the default format.
if (((claim.Type == OpenIdConnectConstants.Claims.Name || claim.Type == OpenIdConnectConstants.Claims.Nickname) && ticket.HasScope(OpenIdConnectConstants.Scopes.Profile)) ||
(claim.Type == OpenIdConnectConstants.Claims.Email && ticket.HasScope(OpenIdConnectConstants.Scopes.Email)) ||
(claim.Type == OpenIdConnectConstants.Claims.Role && ticket.HasScope(OpenIddictConstants.Claims.Roles)) ||
((claim.Type == MyClaimsConstants.Claims.CompanyId || claim.Type == MyClaimsConstants.Claims.CompanyName) && ticket.HasScope(MyClaimsConstants.Scopes.Company))) // <-
{
destinations.Add(OpenIdConnectConstants.Destinations.IdentityToken);
}
claim.SetDestinations(destinations);
}
私はスコープのチェックを追加し、条件付きでUserInfoController
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, MyClaimsConstants.Scopes.Company))
{
claims[MyClaimsConstants.Claims.CompanyId] = user.SelectedCompanyId;
claims[MyClaimsConstants.Claims.CompanyName] = (await _companyRepo.GetCompany(user.SelectedCompanyId))?.CompanyName;
}
にクレームを追加し、それが要求に追加されますので、私のクライアントにAddOpenIdConnectを呼び出すときに、私は私のオプションにスコープを追加します。
options.Scope.Add(MyClaimsConstants.Scopes.Company);
これは意図したとおりに動作しているようです。
誰かが可能ですか?これらのステップにコメントするか、この特定の種類の実装を示すサンプルを指摘してください。 (それは非常に基本的なようですが、私がプロセスで本当に悪いことをしたかどうかは分かりません)。
/THX