public void Persist(ExternalLoginState externalLoginState) { var protector = _dataProtectionProvider.CreateProtector(nameof(ExternalLoginTempService)); var serializedResult = JsonConvert.SerializeObject(externalLoginState); var encryptedResult = protector.Protect(serializedResult); _httpContextAccessor.HttpContext.Response.Cookies.Append(CookieKey, encryptedResult); }
public IActionResult ExternalLogin(string provider, string returnUrl) { var state = _stateHashingService.SetCookieAndReturnHash(); string redirect; ProviderType providerType; switch (provider.ToLower()) { case "facebook": var facebookRedirect = this.FullUrlHelper(nameof(CallbackHandler), Name); redirect = _loginLinkFactory.GetLink(ProviderType.Facebook, facebookRedirect, state, _settingsManager.Current.FacebookAppID); providerType = ProviderType.Facebook; break; case "google": var googleRedirect = this.FullUrlHelper(nameof(CallbackHandler), Name); redirect = _loginLinkFactory.GetLink(ProviderType.Google, googleRedirect, state, _settingsManager.Current.GoogleClientId); providerType = ProviderType.Google; break; case "microsoft": var msftRedirect = this.FullUrlHelper(nameof(CallbackHandler), Name); redirect = _loginLinkFactory.GetLink(ProviderType.Microsoft, msftRedirect, state, _settingsManager.Current.MicrosoftClientID); providerType = ProviderType.Microsoft; break; case "oauth2": var oauthRedirect = this.FullUrlHelper(nameof(CallbackHandler), Name); var linkGenerator = new OAuth2LoginUrlGenerator(); var oauthClaims = new List <string>(new[] { "openid", "email" }); redirect = linkGenerator.GetUrl(_settingsManager.Current.OAuth2LoginUrl, _settingsManager.Current.OAuth2ClientID, oauthRedirect, state, oauthClaims); providerType = ProviderType.OAuth2; break; default: throw new NotImplementedException($"The external login \"{provider}\" is not configured."); } var loginState = new ExternalLoginState { ProviderType = providerType, ReturnUrl = returnUrl }; _externalLoginTempService.Persist(loginState); return(Redirect(redirect)); }