C# ActiveDirectory get PrincipalContext with "Rights"

524 views Asked by At

I have a small web server running ASP.NET MVC on it. The Server is running with User "abc" but the User "abc" do not have rights for "changes" in ActiveDirectory.

So I have to pass the user login in the PrincipalContext with.

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, user, password))
{

    GroupPrincipal theGroup = GroupPrincipal.FindByIdentity(context, groupId);

    theGroup.Members.Add(context, IdentityType.SamAccountName, userId);

    theGroup.Save();

}

the Code does work. But I do not like to transfair a Password from Methode to Methode... => on MVC I have a SSO and the Server knows me

System.Web.HttpContext.Current.User.Identity

It is possible to Use this Information?

new PrincipalContext(ContextType.Domain, null, [System.Web.HttpContext.Current.User]) ???

Or MUST I give the password. And how to best pass from view to this method.

thanks

1

There are 1 answers

3
Gabriel Luci On

This is called "impersonation". As long as you are using Windows authentication, you can do it with the WindowsIdentity.Impersonate() method:

using (var ctx = ((WindowsIdentity) HttpContext.Current.User.Identity).Impersonate()) {
    // Anything done here will use the user's credentials
    using (var context = new PrincipalContext(ContextType.Domain)) {
        ...
    }
}