#pragma warning disable S4261 // Methods should be named according to their synchronicities public async Task <ActionResult> ExternalLoginCallback(string returnUrl) #pragma warning restore S4261 // Methods should be named according to their synchronicities { var loginInfo = await AuthManager.GetExternalLoginInfoAsync(); var user = await _userManager.FindAsync(loginInfo.Login); if (user == null) { user = new UserModel { Email = loginInfo.Email, UserName = loginInfo.DefaultUserName, PersonName = loginInfo.ExternalIdentity.Name }; var result = await _userManager.CreateAsync(user); if (!result.Succeeded) { return(View("Error", result.Errors)); } else { result = await _userManager.AddLoginAsync(user.Id, loginInfo.Login); if (!result.Succeeded) { return(View("Error", result.Errors)); } } } var ident = await _userManager.CreateIdentityAsync( user, DefaultAuthenticationTypes.ApplicationCookie); if (IsBlocked(ident.GetUserId <int>())) { return(View("Blocked")); } ident.AddClaims(loginInfo.ExternalIdentity.Claims); AuthManager.SignOut(); AuthManager.SignIn(new AuthenticationProperties { IsPersistent = false }, ident); AddToActive(user.Id); return(Redirect(GetRedirectUrl(returnUrl))); }
public async Task <IActionResult> TokenSigninGoogle(string user) { var userInfoFromWebApp = JsonConvert.DeserializeObject <GoogleUserInfo>(user); var validatedLoginInfoFromGoogle = await GoogleJsonWebSignature.ValidateAsync(userInfoFromWebApp.IdToken, new GoogleJsonWebSignature.ValidationSettings { Audience = new List <string> { _configuration.GetGoogleClientId() } }); var userInSystem = await _userManager.FindByEmailAsync(validatedLoginInfoFromGoogle.Email); if (userInSystem == null) { userInSystem = new UserModel { Email = validatedLoginInfoFromGoogle.Email, UserName = validatedLoginInfoFromGoogle.Email, PersonName = $"{validatedLoginInfoFromGoogle.FamilyName} {validatedLoginInfoFromGoogle.GivenName}" }; var result = await _userManager.CreateAsync(userInSystem); if (!result.Succeeded) { return(Unauthorized()); } else { result = await _userManager.AddLoginAsync(userInSystem.Id, new UserLoginInfo(GOOGLE, userInSystem.Email)); if (!result.Succeeded) { _userManager.DeleteAsync(userInSystem).Wait(); return(Unauthorized()); } } } var ident = await _userManager.CreateIdentityAsync( userInSystem, DefaultAuthenticationTypes.ApplicationCookie); if (IsBlocked(ident.GetUserId <int>())) { return(Unauthorized()); } ident.AddClaims( new List <Claim> { new Claim(nameof(validatedLoginInfoFromGoogle.Name), validatedLoginInfoFromGoogle.Email), new Claim(nameof(validatedLoginInfoFromGoogle.FamilyName), validatedLoginInfoFromGoogle.FamilyName), new Claim(nameof(validatedLoginInfoFromGoogle.GivenName), validatedLoginInfoFromGoogle.GivenName) }); await HttpContext.SignOutAsync(COOKIE_NAME); await HttpContext.SignInAsync(COOKIE_NAME, new ClaimsPrincipal(ident), new AuthenticationProperties { IsPersistent = false } ); return(Ok()); }