简体   繁体   English

Identity Server 4从外部提供商注册用户

[英]Identity Server 4 Register Users from External Providers

I'm trying to store new users data from the claims return from an external login. 我正在尝试从外部登录返回的索赔中存储新用户数据。

lets just say I have a model 可以说我有一个模特

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

and a method to add a user to the Database: 以及将用户添加到数据库的方法:

_userService.Add(new User()); 

This is the standard IdentityServer implementation for their eternal login call back. 这是其永久登录回叫的标准IdentityServer实现。

[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("~/");
}

My question is how can I retrieve a unique Identifier for each user that logs in? 我的问题是如何为每个登录的用户检索唯一的标识符? Say a user logs in via google, I Cannot use their email address as a unique id because they potentially could already have registered using that email from another provider? 假设用户通过google登录,我不能使用其电子邮件地址作为唯一ID,因为他们可能已经使用其他提供商的电子邮件进行了注册?

You might need to construct some form of identifier that comes from the token issued from the external provider. 您可能需要构造某种形式的标识符,该标识符来自外部提供商发出的令牌。 In OpenID Connect the subject claim is assumed to be locally unique to the provider. 在OpenID Connect中,假定主题声明对于提供者在本地是唯一的。 One way to construct a globally unique identifier is to make some sort of composite key (or constructed key) that uses both the issuer ( iss ) claim and the subject ( sub ) claim of the token, this identifier is generally guaranteed to be globally unique. 构造全局唯一标识符的一种方法是制作某种同时使用令牌的发行方( iss )声明和主题( sub )声明的组合密钥(或构造的密钥),通常保证此标识符是全局唯一的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Thinktecture Identity Server v3如何保持外部提供商的声明? - Thinktecture Identity Server v3 How to keep Claims from external providers? 在 ASP.NET 内核上使用带有身份的 MSAL 注册外部用户 - Register external users using MSAL with Identity on ASP.NET Core 从身份服务器获取用户列表 - Get users list from identity server 如何在本地 IdentityContext 中注册从外部身份验证提供者获得的用户 - How to register in local IdentityContext the users, obtained from external authentication provider 使用来自外部提供程序的ASP.NET Identity和JWT身份验证时,可以使用“角色和声明”的Authorize属性筛选吗? - Can I use the Authorize attribute filtering by Roles and Claims when using ASP.NET Identity and JWT authentication from external providers? 我如何使用 ASP.NET Core 3.1 身份验证和注册来自业务逻辑层的用户 - How do i Authenticate and register users from the business Logic Layer with ASP.NET Core 3.1 Identity Identity Server 4强制注销用户 - Identity Server 4 forced logout users 在Identity Server 4中为OpenID Connect注册IIdentityServerInteractionService - Register IIdentityServerInteractionService in Identity Server 4 for OpenID Connect do.net Web API - MSAL (Microsoft.Identity.Web) 注册多个身份验证提供程序 - dotnet Web API - MSAL ( Microsoft.Identity.Web ) register multiple authentication providers 在外部登录/注册ASP.NET Web API 2.0和Identity期间从Facebook检索其他配置文件信息 - Retrieving additional profile information from Facebook during external login/register with ASP.NET Web API 2.0 and Identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM