public async Task <IActionResult> LinkedinRedirect([FromQuery] string code, [FromQuery] string state)
        {
            var accessToken = _linkedinHelpers.AccessTokenRequest(code);

            if (accessToken == null)
            {
                return(BadRequest(new
                {
                    Error = "User dismissed the authorization"
                }));
            }

            var personModel = await _linkedinHelpers.PersonRequest(accessToken);

            var email = await _linkedinHelpers.EmailRequest(accessToken);

            var user = await _userManager.FindByNameAsync(email);

            if (user != null)
            {
                var userClaims = await this._userManager.GetClaimsAsync(user);

                await HttpContext.SignInAsync("Cookies", userClaims.ToArray());

                return(Ok(new { user = user }));
            }
            else
            {
                var userRegistrationModel = new AppUser
                {
                    UserName = email, Name = personModel.localizedFirstName, Email = email
                };
                var password = PasswordGenerating.GeneratePassword();
                var result   = await _userManager.CreateAsync(userRegistrationModel, password);

                if (!result.Succeeded)
                {
                    return(BadRequest(result.Errors));
                }

                await _userManager.AddToRoleAsync(userRegistrationModel, RoleConstants.DEFAULT_ROLE);

                await _userManager.AddClaimAsync(userRegistrationModel,
                                                 new System.Security.Claims.Claim(ClaimConstants.USERNAME, userRegistrationModel.UserName));

                await _userManager.AddClaimAsync(userRegistrationModel,
                                                 new System.Security.Claims.Claim(ClaimConstants.NAME, userRegistrationModel.Name));

                await _userManager.AddClaimAsync(userRegistrationModel,
                                                 new System.Security.Claims.Claim(ClaimConstants.EMAIL, userRegistrationModel.Email));

                await _userManager.AddClaimAsync(userRegistrationModel,
                                                 new System.Security.Claims.Claim(ClaimConstants.ROLE, RoleConstants.DEFAULT_ROLE));

                await HttpContext.SignInAsync("Cookies",
                                              _userManager.GetClaimsAsync(userRegistrationModel).Result.ToArray());

                return(Ok(new { user = user }));
            }
        }
Esempio n. 2
0
        private async Task <IEnumerable <Claim> > ExternalRegistration(ClaimsPrincipal externalUser)
        {
            var email = externalUser.FindFirstValue(ClaimTypes.Email);
            var name  = externalUser.FindFirstValue(ClaimTypes.Name);

            var user = new AppUser {
                UserName = email, Name = name, Email = email
            };
            var password = PasswordGenerating.GeneratePassword();

            await CreateUser(user, password);

            return(await _userManager.GetClaimsAsync(user));
        }