2017-03-12 1 views
0

外部ログインからのクレーム返品から新しいユーザーデータを格納しようとしています。Identity Server 4外部プロバイダからのユーザーの登録

_userService.Add(new User()); 

これが戻って自分の永遠のログインコールのための標準IdentityServer実装です:

は私がモデル

public class User 
{ 
    Guid UniqueIdentifier; 
    string Username; 
    string Firstname; 
    string LastName; 
    string Email; 
    Date DateOfBirth; 
} 

とデータベースにユーザーを追加する方法を持っているだけで言うことができます。

[HttpGet] 
public async Task<IActionResult> ExternalLoginCallback(string returnUrl) 
{ 
    // read external identity from the temporary cookie 
    var info = await HttpContext.Authentication.GetAuthenticateInfoAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme); 
    var tempUser = info?.Principal; 
    if (tempUser == null) 
    { 
     throw new Exception("External authentication error"); 
    } 

    // retrieve claims of the external user 
    var claims = tempUser.Claims.ToList(); 

    // try to determine the unique id of the external user - the most common claim type for that are the sub claim and the NameIdentifier 
    // depending on the external provider, some other claim type might be used 
    var userIdClaim = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Subject); 
    if (userIdClaim == null) 
    { 
     userIdClaim = claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier); 
    } 
    if (userIdClaim == null) 
    { 
     throw new Exception("Unknown userid"); 
    } 

    // remove the user id claim from the claims collection and move to the userId property 
    // also set the name of the external authentication provider 
    claims.Remove(userIdClaim); 
    var provider = info.Properties.Items["scheme"]; 
    var userId = userIdClaim.Value; 

    // check if the external user is already provisioned 
    var user = await _userManager.FindByLoginAsync(provider, userId); 
    if (user == null) 
    { 
     user = new IdentityUser { UserName = Guid.NewGuid().ToString() 
    }; 

    await _userManager.CreateAsync(user); 

    await _userManager.AddLoginAsync(user, new UserLoginInfo(provider, userId, provider)); 
    } 

    var additionalClaims = new List<Claim>(); 

    // if the external system sent a session id claim, copy it over 
    var sid = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.SessionId); 
    if (sid != null) 
    { 
     additionalClaims.Add(new Claim(JwtClaimTypes.SessionId, sid.Value)); 
    } 

    // issue authentication cookie for user 
    await HttpContext.Authentication.SignInAsync(user.Id, user.UserName, provider, additionalClaims.ToArray()); 

    // delete temporary cookie used during external authentication 
    await HttpContext.Authentication.SignOutAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme); 

    // validate return URL and redirect back to authorization endpoint 
    if (_interaction.IsValidReturnUrl(returnUrl)) 
    { 
     return Redirect(returnUrl); 
    } 

    return Redirect("~/"); 
} 

私の質問は、ログインする各ユーザーごとに一意の識別子を取得する方法です。ユーザーがgoogle経由でログインしたとします。自分のメールアドレスを一意のIDとして使用することはできません。

答えて

0

外部プロバイダから発行されたトークンに由来する何らかの形式の識別子を構築する必要があります。 OpenID Connectでは、主題主張はプロバイダにローカルで一意であるとみなされます。グローバルにユニークな識別子を構成する1つの方法は、発行者(iss)クレームとトークンのサブジェクト(sub)クレームの両方を使用する何らかの複合キー(または構成キー)を作成することです。この識別子は一般にグローバルに一意であることが保証されています。

+0

ありがとうございます。例を挙げてください。 –

関連する問題