Created
March 20, 2013 23:45
-
-
Save ChrisMcKee/5209542 to your computer and use it in GitHub Desktop.
module to cleanse a user of temporary authentication by boxing them within a controller.
Theres more elegant ways of handling this such as creating custom user
This file contains hidden or 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
namespace xxx.Web.Auth | |
{ | |
using System; | |
using System.Web; | |
using xxx.Domain.Entities; | |
static class ContextExtension | |
{ | |
public static bool IsValidForInterrogation(this HttpContext context) | |
{ | |
return context.Request.RequestContext.RouteData != null | |
&& context.Request.RequestContext.RouteData.Values != null | |
&& context.Request.RequestContext.RouteData.Values.ContainsKey("controller"); | |
} | |
public static bool IsMvcRequest(this HttpContext context) | |
{ | |
return context.Request.RequestContext.RouteData != null && | |
context.Request.RequestContext.RouteData.RouteHandler != null; | |
} | |
} | |
internal class AuthenticationModule : IHttpModule | |
{ | |
public void Init(HttpApplication context) | |
{ | |
context.PostAcquireRequestState += ContextOnPostAcquireRequestState; | |
} | |
private void ContextOnPostAcquireRequestState(object sender, EventArgs eventArgs) | |
{ | |
HttpApplication application = (HttpApplication)sender; | |
HttpContext context = application.Context; | |
if ((context != null && context.Request.IsAuthenticated)) | |
{ | |
var isBoxedController = context.IsValidForInterrogation() && context.Request.RequestContext.RouteData.Values["controller"].Equals("SpecialControllerForSpecialPeople"); | |
if (!isBoxedController && context.IsMvcRequest()) | |
{ | |
var auth = context.User.Identity; | |
if (auth.CurrentRole().HasValue && auth.CurrentRole().GetValueOrDefault() == RoleType.UnactivatedAccount) | |
{ | |
if (context.Response.Cookies["SPECIALCOOKIE"] != null) | |
{ | |
context.Response.Cookies["SPECIALCOOKIE"].Expires = DateTime.MinValue; | |
} | |
System.Web.Security.FormsAuthentication.SignOut(); | |
context.Session.Abandon(); | |
} | |
} | |
} | |
} | |
public void Dispose() | |
{ | |
} | |
} | |
} |
This file contains hidden or 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
You'd need to add this module to your httpmodules and again in the httpmodules within the IIS7 config section. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment