Created
February 23, 2017 07:33
-
-
Save vbookie/4eee7a9d286c9febe235ca3a4cdc9838 to your computer and use it in GitHub Desktop.
Custom external authentication provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomGoogleAuthenticationProvider : SitefinityGoogleAuthenticationProvider | |
{ | |
public override Task Authenticated(GoogleOAuth2AuthenticatedContext context) | |
{ | |
var isIdentityValid = YourCustomCheck(); | |
if (!isIdentityValid) | |
{ | |
context.Identity = null; | |
return Task.FromResult(0); | |
} | |
return base.Authenticated(context); | |
} | |
} | |
public class CustomAuthenticationProvidersInitializer : AuthenticationProvidersInitializer | |
{ | |
public override Dictionary<string, Action<IAppBuilder, string, AuthenticationProviderElement>> GetAdditionalIdentityProviders() | |
{ | |
var providers = base.GetAdditionalIdentityProviders(); | |
providers["Google"] = | |
(IAppBuilder app, string signInAsType, AuthenticationProviderElement config) => | |
{ | |
var googleConfig = config as GoogleAuthenticationProviderElement; | |
if (googleConfig != null) | |
{ | |
var opt = new GoogleOAuth2AuthenticationOptions | |
{ | |
AuthenticationType = googleConfig.Name, | |
Caption = googleConfig.Title, | |
SignInAsAuthenticationType = signInAsType, | |
ClientId = googleConfig.AppId, | |
ClientSecret = googleConfig.AppSecret, | |
Provider = new CustomGoogleAuthenticationProvider() | |
}; | |
app.UseGoogleAuthentication(opt); | |
} | |
}; | |
return providers; | |
} | |
} | |
public class Global : System.Web.HttpApplication | |
{ | |
protected void Application_Start(object sender, EventArgs e) | |
{ | |
SystemManager.ApplicationStart += SystemManager_ApplicationStart; | |
} | |
private void SystemManager_ApplicationStart(object sender, EventArgs e) | |
{ | |
ObjectFactory.Container.RegisterType<AuthenticationProvidersInitializer, CustomAuthenticationProvidersInitializer>(new ContainerControlledLifetimeManager()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment