Skip to content

Instantly share code, notes, and snippets.

@vbookie
Created February 23, 2017 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbookie/4eee7a9d286c9febe235ca3a4cdc9838 to your computer and use it in GitHub Desktop.
Save vbookie/4eee7a9d286c9febe235ca3a4cdc9838 to your computer and use it in GitHub Desktop.
Custom external authentication provider
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