2017-04-21 1 views
1

사용자가 Google을 통해 IdentityServer4에 로그인 할 때 이메일을 액세스하고 싶지만 클라이언트가 요청하지 않아도됩니다. 따라서 매번 액세스 할 수 있어야하므로 API에 사용자의 이메일 주소가 필요하기 때문에 access_token에 넣을 수 있습니다.IdentityServer4 : 클라이언트가 명시 적으로 요청하지 않고 access_token에 전자 메일을 포함하는 방법은 무엇입니까?

나는 IProfileService에 주사 했으므로 IClaimsService도 있지만 이메일을 찾을 수 없습니다. 수동으로 응답에 액세스 할 수 있도록 Google-SignIn 콜백에 연결할 수 있습니까?

감사합니다.

+0

Google 로그인에서 반환 된 Id 토큰에는 구체적으로 묻지 않고 이미 이메일이 포함되어 있습니다 ... https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo – Mashton

답변

0

난이 같은 AccountController.ExternalLoginCallback 필요한 주장을 첨가하여 해결할 :

을 : 내 ProfileService 클래스를 주입하고이 같은 MyProfileService.GetProfileDataAsync 주장을 추가 종속하여 access_token 상기 제를 첨가 한 다음
//Add E-Mail claim even if client didn't ask for it 
if (claims.Exists(c => c.Type.Equals(ClaimTypes.Email))) { 
    additionalClaims.Add(new Claim(JwtClaimTypes.Email, claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Email)).Value)); 
} 

public Task GetProfileDataAsync(ProfileDataRequestContext context) 
{ 
    var claims = new List<Claim>(); 

    Claim emailClaim = context.Subject.Claims.Where<Claim>(claim => claim.Type.Equals(JwtClaimTypes.Email)).FirstOrDefault(); 

    if (emailClaim != null) 
    { 
     claims.Add(emailClaim); 
    } 

    context.IssuedClaims = claims; 
    return Task.FromResult(0); 
} 
관련 문제