Custom IPrincipal with Windows Authentication

Is there a good way to combine ASP.NET Windows authentication with a custom IPrincipal / IIdentity object? I need to save the user's email address and do this for authentication using the special IIdentity / IPrincipal pair that I added to Context.CurrentUser during the AuthenticateRequest event.

What is the best way for me to do this using WindowsAuthentication?

+5
source share
2 answers

Perhaps you could create your "ExtendedWindowsPrincipal" as a derived class based on WindowsPrincipal and just add your extra data to the derived class?

That way, your ExtendedWindowsPrincipal will still be recognized wherever WindowsPricinpal is required.

OR: since you are talking about using Windows authentication, you are probably on a Windows network — is there somewhere in the Active Directory or user database where you can find the email address you are interested in instead of storing it in the main one?

Mark

+3
source

Identity, . Identity , WindowsPrincipal.

public class ExtendedWindowsPrincipal : WindowsPrincipal
{
    private readonly string _email;

    public ExtendedWindowsPrincipal(WindowsIdentity ntIdentity, 
       string email) : base(ntIdentity)
    {
            _email = email;
    }

    public string Email
    {
        get { return _email; }
    }
}

HttpContext :

var currentUser = (WindowsIdentity)HttpContext.Current.User.Identity;
HttpContext.Current.User = 
    new ExtendedWindowsPrincipal(currentUser, userEmail);
+3

All Articles