How to use a Custom Principal and Identity with ASP.NET MembershipProvider - Peter's Software House

How to use a Custom Principal and Identity with ASP.NET MembershipProvider

Using the ASP.NET membership provider?  Love the ease and convenience?  BUT, you really want to change the current HttpContent Principal and/or Identity?

Here is one way of doing it.  In your Global.ascx add the following piece of code.  Replace the CustomIdentity and CustomPrincipal with you own implementations.

protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
    if (Context.User != null)
    {
        if (Context.User.Identity.IsAuthenticated)
            {
                if (Context.User.Identity is FormsIdentity)
                {
                    RolePrincipal principal = (RolePrincipal)HttpContext.Current.User;
                    FormsIdentity id = (FormsIdentity)principal.Identity;
                    CustomIdentity ident = new CustomIdentity(id);
                    Context.User = new CustomPrincipal( ident );
                }
            }
        }  
    }
}

 

You don’t need all the casts in there, this is just to show what all the different principals and identities are.

Published Thursday, March 05, 2009 3:15 PM by Pieter
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: