public async Task <IActionResult> loginexternalcallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { _logger.LogError($"Error from external provider: {remoteError}"); return(RedirectToAction(nameof(login))); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { _logger.LogDebug($"GetExternalLoginInfoAsync() returned null"); return(RedirectToAction(nameof(login))); } _logger.LogInformation($"Callback from LoginProvider={info.LoginProvider} ProviderKey={info.ProviderKey} ProviderDisplayName={info.ProviderDisplayName}"); foreach (var claim in info.Principal.Claims) { _logger.LogDebug($"Claim: Type={claim.Type} Value={claim.Value} Issuer={claim.Issuer}"); } // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (result.Succeeded) { _logger.LogInformation("Login succeeded; user already has login"); // TODO: fetch image return(RedirectToLocal(returnUrl)); } if (result.IsLockedOut) { // TODO: handle locked out case _logger.LogInformation("User is locked out"); return(RedirectToAction(nameof(login))); } else { _logger.LogInformation("Need to create login for user; redirecting"); return(RedirectToAction(nameof(registerexternal))); } }
public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (result.Succeeded) { _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); return(LocalRedirect(returnUrl)); } if (result.IsLockedOut) { return(RedirectToPage("./Lockout")); } else { // If the user does not have an account, then ask the user to create an account. ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) { Input = new InputModel { Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; } return(Page()); } }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { ModelState.AddModelError(string.Empty, _localizer["ErrorExternalProvider", remoteError]); return(View(nameof(Login))); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(RedirectToAction(nameof(Login))); } // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false); if (result.Succeeded) { return(RedirectToLocal(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToAction(nameof(LoginWith2fa), new { ReturnUrl = returnUrl })); } if (result.IsLockedOut) { return(View("Lockout")); } // If the user does not have an account, then ask the user to create an account. ViewData["ReturnUrl"] = returnUrl; ViewData["LoginProvider"] = info.LoginProvider; var email = info.Principal.FindFirstValue(ClaimTypes.Email); var userName = info.Principal.Identity.Name; return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email, UserName = userName })); }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToAction(nameof(Login))); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(RedirectToAction(nameof(Login))); } // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (result.Succeeded) { _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider); return(RedirectToLocal(returnUrl)); } if (result.IsLockedOut) { return(RedirectToAction(nameof(Lockout))); } else { // If the user does not have an account, then ask the user to create an account. ViewData["ReturnUrl"] = returnUrl; ViewData["LoginProvider"] = info.LoginProvider; var email = info.Principal.FindFirstValue(ClaimTypes.Email); return(View("ExternalLogin", new ExternalLoginViewModel { Email = email })); } }
public async Task <SignInResult> LoginExternal(ExternalLoginInfo info) { return(await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true)); }
public async Task <LoginResponseResult> ExternalLogin([FromBody] LoginRequest request) { try { var loginProvider = request.Key; string providerKey = request.Value; _logger.LogInformation($"loginProvider:{loginProvider},providerKey:{providerKey}"); var signInResult = await _signInManager.ExternalLoginSignInAsync( loginProvider, providerKey, false); _logger.LogInformation($"signInResult:{signInResult.Succeeded}"); if (signInResult.Succeeded) { var user = await _userManager.FindByLoginAsync(loginProvider, providerKey); string token = await this.GetToken(user); _logger.LogInformation($"tokenresponse:{token}"); return(new LoginResponseResult() { Code = 200, AccessToken = token, Data = user }); } else if (signInResult.IsLockedOut) { return(new LoginResponseResult() { Code = (int)System.Net.HttpStatusCode.Locked, //423 Message = "User is locked. Please contact Administrator for unlock. " }); } else if (signInResult.IsNotAllowed) { return(new LoginResponseResult() { Code = (int)System.Net.HttpStatusCode.MethodNotAllowed, //405 Message = "User is not allowed. " }); } else if (signInResult.RequiresTwoFactor) { return(new LoginResponseResult() { Code = (int)System.Net.HttpStatusCode.ExpectationFailed, //417 Message = "Two-Factor sign-in is required. " }); } } catch (Exception ex) { _logger.LogError(ex, "SignInController.externallogin", request); } return(new LoginResponseResult() { Code = 401 }); }
public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) { returnUrl ??= Url.Content("~/"); if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } // Sign in the user with this external login provider if the user already has a login. var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, false, true); if (result.Succeeded) { _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); return(LocalRedirect(returnUrl)); } if (result.IsLockedOut) { return(RedirectToPage("./Lockout")); } // If the user does not have an account, then create an account automatically. ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) { var createResult = await _externalAccountHelper.CreateUser(info); if (createResult.Succeeded) { var principal = info.Principal; Input = new InputModel { Email = principal.FindFirstValue(ClaimTypes.Email), PhoneNumber = principal.FindFirstValue(ClaimTypes.MobilePhone) ?? principal.FindFirstValue(ClaimTypes.HomePhone) ?? principal.FindFirstValue(ClaimTypes.OtherPhone), GivenName = principal.FindFirstValue(ClaimTypes.GivenName), FamilyName = principal.FindFirstValue(ClaimTypes.Surname) }; } else { foreach (var error in createResult.Errors) { if (error.Code == "DuplicateUserName") { ShowVerifyEmailLink = true; // skip to avoid error message for username AND email continue; } ModelState.AddModelError(string.Empty, error.Description); } } } return(Page()); }
public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToAction(nameof(Login))); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return(RedirectToAction(nameof(Login))); } //Verifica se usuário já está cadastrado na base de dados da aplicação var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (result.Succeeded) { _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider); return(RedirectToAction(nameof(EstudanteController.Roll), "Estudante")); } if (result.IsLockedOut) { return(RedirectToAction(nameof(Lockout))); } // If the user does not have an account, then ask the user to create an account. ViewData["ReturnUrl"] = returnUrl; ViewData["LoginProvider"] = info.LoginProvider; var email = info.Principal.FindFirstValue(ClaimTypes.Email); var phone = info.Principal.FindFirstValue(ClaimTypes.MobilePhone); var name = info.Principal.FindFirstValue(ClaimTypes.GivenName); var surname = info.Principal.FindFirstValue(ClaimTypes.Surname); var birthday = info.Principal.FindFirstValue(ClaimTypes.DateOfBirth); var gender = info.Principal.FindFirstValue(ClaimTypes.Gender); var location = info.Principal.FindFirstValue(ClaimTypes.Locality); var identifier = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier); var picture = $"https://graph.facebook.com/{identifier}/picture?type=large"; var externalLoginViewModel = new ExternalLoginViewModel { GeneroId = gender == "male" ? 1 : gender == "female" ? 2 : 1, Nome = name, Sobrenome = surname, DataNascimento = DateTime.Parse(birthday, new CultureInfo("en-US")), Email = email, PhoneNumber = phone, Localizacao = location, CaminhoFoto = picture, Cursos = new SelectList(_cursoAppService.GetAll().OrderBy(x => x.Nome), "Id", "Nome"), OrientacoesPoliticas = new SelectList(_estudanteAppService.ListarOrientacoesPoliticas(), "Id", "Nome"), TiposDeConsumoBebida = new SelectList(_estudanteAppService.ListarTiposDeConsumoBebida(), "Id", "Nome"), TiposDeAtracao = _estudanteAppService.ListarTiposDeAtracao().Select(x => new CheckBoxListItem { Id = x.Id, Text = x.Nome }).ToArray(), GenerosInteresse = new[] { new CheckBoxListItem { Id = 1, Text = "Homens" }, new CheckBoxListItem { Id = 2, Text = "Mulheres" }, } }; return(View(nameof(ExternalLogin), externalLoginViewModel)); }