Question

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.

Was it helpful?

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;
            }
        }
    }

OTHER TIPS

Create an interface/class to wrap the access to the current user object. Your custom service can return the user object or whatever you would like, and it will be easy to mock up in tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top