C# accessing active directory with different user credentials

前端 未结 4 1657
盖世英雄少女心
盖世英雄少女心 2020-12-19 07:12

There is a new user creation application that we have just provided our users. However these users need the ability to creation users through the application even though the

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 07:41

    You can use the DirectoryEntry class directly and specify the username and password:

    DirectoryEntry de = new DirectoryEntry(path);
    
    de.Username = "username";
    de.Password = "password";
    

    And access Active Directory from the de object. Or you can use the WindowsIdentity class and and impersonate a User:

    WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
    WindowsImpersonationContext impersonatedUser = newId.Impersonate();
    

    A full code sample is available at:

    Impersonation and DirectoryEntry

提交回复
热议问题