Created
February 12, 2019 03:19
-
-
Save jlucaspains/1427eb538f90ad9cd272b4dde07a55cf to your computer and use it in GitHub Desktop.
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
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