public RedirectResult RedirectToProvider(RedirectToProviderInputModel inputModel) { #region Input Model Validation if (!ModelState.IsValid) { throw new ArgumentException( "Some binding errors occured. This means at least one Request value (eg. form post or querystring parameter) provided is invalid. Generally, we need a ProviderName as a string."); } if (string.IsNullOrEmpty(inputModel.ProviderName)) { throw new ArgumentException( "ProviderKey value missing. You need to supply a valid provider key so we know where to redirect the user Eg. google."); } Uri identifier = null; if (!string.IsNullOrEmpty(inputModel.Identifier) && !Uri.TryCreate(inputModel.Identifier, UriKind.Absolute, out identifier)) { throw new ArgumentException("The Identifier value [" + inputModel.Identifier + "] is not a valid Uri. Please fix it up. eg. http://goto.some.website/authenticate/"); } #endregion // Grab the Provider. var provider = GetAuthenticationProvider(inputModel.ProviderName); // Most providers don't need any pre-setup crap, to redirect to authenticate. // But of course, there's always one - OpenId. We have no idea WHERE we want to // redirect to, so we need to do a particular check here. // Of course, any value here could be used for any other provider. But that would be weird. // TODO: Confirm this is not a security threat / open to abuse in some way. if (identifier != null) { provider.AuthenticateRedirectionUrl = identifier; } // Where do we return to, after we've authenticated? var callbackUri = GenerateCallbackUri(provider.Name, _configurationOptions?.BasePath); // Determine where we need to redirect to. var redirectToAuthenticateSettings = provider.RedirectToAuthenticate(callbackUri); if (redirectToAuthenticateSettings == null) { // We failed to determine where to go. A classic example of this is with OpenId and a bad OpenId endpoint. const string errorMessage = "No redirect to authencate settings retrieved. This means we don't know where to go. A classic example of this is with OpenId and a bad OpenId endpoint. Please check the data you are providing to the Controller. Otherwise, you will need to debug the individual provider class you are trying use to connect with."; TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } // Remember any important information for after we've come back. _cache[SessionKeyState] = redirectToAuthenticateSettings.State; _cache[SessionKeyReturnToUrl] = DetermineReturnUrl(inputModel.ReturnUrl); _cache[SessionKeyRedirectToProviderUrl] = redirectToAuthenticateSettings.RedirectUri.AbsoluteUri; // Now redirect :) return Redirect(redirectToAuthenticateSettings.RedirectUri.AbsoluteUri); }
public RedirectResult RedirectToProvider(RedirectToProviderInputModel inputModel) { #region Input Model Validation if (!ModelState.IsValid) { throw new ArgumentException( "Some binding errors occured. This means at least one Request value (eg. form post or querystring parameter) provided is invalid. Generally, we need a ProviderName as a string."); } if (string.IsNullOrEmpty(inputModel.ProviderName)) { throw new ArgumentException( "ProviderKey value missing. You need to supply a valid provider key so we know where to redirect the user Eg. google."); } Uri identifier = null; if (!string.IsNullOrEmpty(inputModel.Identifier) && !Uri.TryCreate(inputModel.Identifier, UriKind.Absolute, out identifier)) { throw new ArgumentException("The Identifier value [" + inputModel.Identifier + "] is not a valid Uri. Please fix it up. eg. http://goto.some.website/authenticate/"); } #endregion // Grab the Provider. var provider = GetAuthenticationProvider(inputModel.ProviderName); // Most providers don't need any pre-setup crap, to redirect to authenticate. // But of course, there's always one - OpenId. We have no idea WHERE we want to // redirect to, so we need to do a particular check here. // Of course, any value here could be used for any other provider. But that would be weird. // TODO: Confirm this is not a security threat / open to abuse in some way. if (identifier != null) { provider.AuthenticateRedirectionUrl = identifier; } // Where do we return to, after we've authenticated? var callbackUri = GenerateCallbackUri(provider.Name); // Determine where we need to redirect to. var redirectToAuthenticateSettings = provider.RedirectToAuthenticate(callbackUri); if (redirectToAuthenticateSettings == null) { // We failed to determine where to go. A classic example of this is with OpenId and a bad OpenId endpoint. const string errorMessage = "No redirect to authencate settings retrieved. This means we don't know where to go. A classic example of this is with OpenId and a bad OpenId endpoint. Please check the data you are providing to the Controller. Otherwise, you will need to debug the individual provider class you are trying use to connect with."; TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } // Remember any important information for after we've come back. Session[SessionKeyState] = redirectToAuthenticateSettings.State; Session[SessionKeyReturnToUrl] = DetermineReturnUrl(inputModel.ReturnUrl); Session[SessionKeyRedirectToProviderUrl] = redirectToAuthenticateSettings.RedirectUri.AbsoluteUri; // Now redirect :) return(Redirect(redirectToAuthenticateSettings.RedirectUri.AbsoluteUri)); }