2017-09-25 1 views
1

저는 OpenId와 MVC에서의 인증에 상당히 익숙하며 인증 코드 플로우 샘플에서 볼 수 있듯이 OpenIddict 인증 서버와 클라이언트 mvc 응용 프로그램을 만듭니다. https://github.com/openiddict/openiddict-samples 내 애플리케이션 사용자의 회사 ID를 소유권 주장에 추가해야하고 필요한 단계를 파악하려고합니다.OpenIddict MVC Core 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); 

이것은 의도 한대로 작동하는 것 같습니다.

누군가가 가능합니까? 이 단계에 대해 의견을 제시하거나이 특정 유형의 구현을 보여주는 샘플을 지적하십시오. (그것은 매우 기본적인 것처럼 보이지만 나는 그 과정에서 정말로 나쁜 것을했는지 전혀 모른다.)

/들으

답변

0

이 의도 한대로 작동하는 것 같군.

실제로 사용자 지정 범위를 구현해야하는 방식이기 때문입니다.

최신 OpenIddict 버전에서는 클라이언트 응용 프로그램이 범위가 공개적으로 지원되는지 여부를 쉽게 확인할 수 있도록 서버가 지원하는 표준 및 사용자 지정 범위를 검색 문서 (예 : http://[host]/.well-known/openid-configuration을 통해 검색 할 수 있음)에 표시 할 수 있습니다. 또는 :

services.AddOpenIddict(options => 
{ 
    // ... 

    options.RegisterScopes(
     OpenIdConnectConstants.Scopes.Profile, 
     OpenIdConnectConstants.Scopes.Email, 
     MyClaimsConstants.Scopes.Company); 
}); 
관련 문제