Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jlucaspains/1427eb538f90ad9cd272b4dde07a55cf to your computer and use it in GitHub Desktop.
Save jlucaspains/1427eb538f90ad9cd272b4dde07a55cf to your computer and use it in GitHub Desktop.
using System;
using System.IdentityModel.Services;
using System.Security.Claims;
using System.Threading;
using System.Web;
namespace CustomAuth
{
public class CacheCustomAuthenticationHttpModule : IHttpModule
{
public void Dispose()
{
// nothing to dispose...
}
public void Init(HttpApplication context)
{
// wire up our custom authentication after default Windows Authentication
context.PostAuthenticateRequest += Context_PostAuthenticateRequest;
}
void Context_PostAuthenticateRequest(object sender, EventArgs e)
{
var context = ((HttpApplication)sender).Context;
// no need to call transformation if session already exists
if (FederatedAuthentication.SessionAuthenticationModule != null &&
FederatedAuthentication.SessionAuthenticationModule.ContainsSessionTokenCookie(context.Request.Cookies))
{
return;
}
// find our CustomAuthenticationManager instance
var transformer = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager;
if (transformer != null)
{
// Invoke authentication which will process/transform claims
var transformedPrincipal = transformer.Authenticate(context.Request.RawUrl, context.User as ClaimsPrincipal);
// set context and thread user
context.User = transformedPrincipal;
Thread.CurrentPrincipal = transformedPrincipal;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment