예제 #1
0
        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();
            Claim userIdClaim = GetUserIdClaim(claims);

            // 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 providerType   = info.Properties.Items["scheme"];
            var providerUserId = userIdClaim.Value;

            // check if the external user is already provisioned
            var user = await _userStore.GetUserByLoginAsync(providerType, providerUserId);

            if (user == null)
            {
                // this sample simply auto-provisions new external user
                // another common approach is to start a registrations workflow first
                //user = _userStore.AutoProvisionUser(providerId, userId, claims);
                user = new User
                {
                    Role     = RoleNames.Player,
                    IsActive = true,
                    Logins   = new[] {
                        new Login
                        {
                            ProviderType = providerType,
                            ProviderId   = providerUserId
                        }
                    }
                };
                _logger.LogInformation("Creating user from external source '{0}'", providerType);
                await _userStore.SaveUserAsync(user);
            }
            // Check for a gamertag and render registration page if not
            var gamertag = await _playerManagementClient.GetGamertagForUserIdAsync(user.UserId);

            if (string.IsNullOrEmpty(gamertag))
            {
                return(View("Register", new RegisterViewModel
                {
                    ReturnUrl = returnUrl
                }));
            }

            return(await SwitchToNetherAuthAndRedirectAsync(returnUrl, info, claims, providerType, user, gamertag));
        }
        public async Task <IEnumerable <Claim> > GetUserClaimsAsync(User user)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.UserId),
                new Claim(JwtClaimTypes.Name, user.UserId),
                new Claim(JwtClaimTypes.Role, user.Role),
            };

            var gamertag = await _playerManagementClient.GetGamertagForUserIdAsync(user.UserId);

            if (!string.IsNullOrEmpty(gamertag))
            {
                claims.Add(new Claim(JwtClaimTypes.NickName, gamertag));
            }

            return(claims);
        }