Search code examples
c#asp.net-mvcldapimpersonationiprincipal

Impersonating a user in ASP.NET MVC for testing


Is there an easy way to substitute current User object (the one inside controller) with IPrincipal having properties of another user? I'm thinking about environment that users Windows authentication and AD groups, so it's desirable to replicate all AD properties.

The "hard" way is to do LDAP query and implement IPrincipal interface, but I want to avoid that.


Solution

  • I solved this by adding a property like this to my base controller:

        new public IPrincipal User
        { //we override User for impersonation
            get {
                if (/*check for impersonation*/)
                {
                    return /*impersonated*/;
                }
                else
                {
                    return base.User;
                }
            }
        }