public IEnumerable<ExternalLoginViewModel> ExternalLogins(string returnUrl, bool generateState = false) { IEnumerable<AuthenticationDescription> descriptions = Authentication.GetExternalAuthenticationTypes(); List<ExternalLoginViewModel> logins = new List<ExternalLoginViewModel>(); string state; if (generateState) { state = GenerateAntiForgeryState(); } else { state = null; } foreach (AuthenticationDescription description in descriptions) { ExternalLoginViewModel login = new ExternalLoginViewModel { Name = description.Caption, Url = Url.Route("ExternalLogin", new { provider = description.AuthenticationType, response_type = "token", client_id = Startup.PublicClientId, redirect_uri = returnUrl, state = state }), State = state }; logins.Add(login); } return logins; }
public IEnumerable<ExternalLoginViewModel> GetExternalLogins(string returnUrl, bool generateState = false) { IEnumerable<AuthenticationDescription> descriptions = Authentication.GetExternalAuthenticationTypes(); List<ExternalLoginViewModel> logins = new List<ExternalLoginViewModel>(); string state; if (generateState) { const int strengthInBits = 256; state = RandomOAuthStateGenerator.Generate(strengthInBits); } else { state = null; } foreach (AuthenticationDescription description in descriptions) { string relativeUrl = Url.Route("ExternalLogin", new { provider = description.AuthenticationType, response_type = "token", client_id = Startup.PublicClientId, // TODO: get redirect uri from store based upon something redirect_uri = "http://localhost:4000/", state = state }); ExternalLoginViewModel login = new ExternalLoginViewModel { Name = description.Caption, Url = new Uri(Request.RequestUri, relativeUrl).AbsoluteUri, State = state }; logins.Add(login); } return logins; }
public IEnumerable<ExternalLoginViewModel> GetExternalLogins(string returnUrl, bool generateState = false) { IEnumerable<AuthenticationDescription> descriptions = Authentication.GetExternalAuthenticationTypes(); List<ExternalLoginViewModel> logins = new List<ExternalLoginViewModel>(); string state; if (generateState) { const int strengthInBits = 256; state = RandomOAuthStateGenerator.Generate(strengthInBits); } else { state = null; } foreach (AuthenticationDescription description in descriptions) { ExternalLoginViewModel login = new ExternalLoginViewModel { Name = description.Caption, Url = Url.Route("ExternalLogin", new { provider = description.AuthenticationType, response_type = "token", client_id = Startup.PublicClientId, redirect_uri = new Uri(Request.RequestUri, returnUrl).AbsoluteUri, //redirect_uri = HttpUtility.UrlPathEncode(returnUrl), state = state }), State = state }; logins.Add(login); } return logins; }
public async Task <IActionResult> ExternalLoginCallback(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.LogError($"Could not get external login info"); 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) { await _signInManager.UpdateExternalAuthenticationTokensAsync(info); _logger.LogInformation("User logged in with {Name} provider.", info.LoginProvider); return(RedirectToLocal(returnUrl)); } if (result.IsLockedOut) { _logger.LogError($"User is locked out"); return(RedirectToAction(nameof(Login))); //return RedirectToAction(nameof(Lockout)); } else { ExternalLoginViewModel model = new ExternalLoginViewModel(); IUser existingUser = null; model.Email = info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue(OpenIdConnectConstants.Claims.Email); if (model.Email != null) { existingUser = await _userManager.FindByEmailAsync(model.Email); } if (model.IsExistingUser = (existingUser != null)) { model.UserName = existingUser.UserName; } else { model.UserName = info.Principal.FindFirstValue(ClaimTypes.GivenName) ?? info.Principal.FindFirstValue(OpenIdConnectConstants.Claims.GivenName) ?? info.Principal.FindFirstValue(OpenIdConnectConstants.Claims.Name); } // If the user does not have an account, check if he can create an account. if ((!model.IsExistingUser && !(await _siteService.GetSiteSettingsAsync()).As <RegistrationSettings>().UsersCanRegister)) { _logger.LogInformation("Site does not allow user registration."); return(NotFound()); } ViewData["ReturnUrl"] = returnUrl; ViewData["LoginProvider"] = info.LoginProvider; return(View("ExternalLogin", model)); } }