BolnoYGaD
BolnoYGaD

Reputation: 31

C# Ask for Domain Admin credential and use them to perform some task

I need some help with examples how to use Credential of a current user running application. So in windows 7 you can run application using user loged in by simply running application or you can use "Run as a different User" option and run it as another user.

In my Active Directory I have 2 account Domain User and one with Domain Admin rights. I'm login Windows as a Domain User and when I need I'm using "Run as a different User" to launch some task as a Domain Admin.

So the task is to get my Credential and use it to perform some task, lets say rename active directory user name. Best way to do this as I can see is to ask user running application to enter Domain Admin credential on then start application and use them for various task. Of course I can easily run application with "Run as a different User" but I still need to get this credential and use them.

I've searched through the web and I can't find this, all i could find is using credential for a web auth.

If you can show me some examples how to:

1) Ask user for a Admin user credential ( i can leave without this )

2) Get and use credentials of a user running application

I don't want to know password I know I can't. Don't really want to add to a WPF form password box I prefer to use windows API to handle this i've already entered user name and password using "Run as a different User".

PS: I sorry if this topic exists :( I guess I'm bad at creating correct search requests.

ADDED: to be more clear what I need. In powershell it will look like this:

# This Asks user to enter credentials
$cred = Get-Credential; 
# this checks if I have rights to use them.
Get-ADDomain “DOMAIN” –Server “Domain.com” –Credential $cred; 

Of course it's simplified as hell though the point is that I can use credentials user entered when ever it's needed.

Upvotes: 1

Views: 1775

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127593

The equivalent C# to your Get-ADDomain is quite simple, it is just

public void PerformSomeActionAsAdmin(string adminUsername, string adminPassword)
{
    //Null causes the constructor to connect to the current domain the machine is on.
    //                                                                       |
    //                                                                       V
     using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, adminUsername, adminPassword))
    {
        //do something here with ctx, the operations will be performed as whoever's username and password you passed in.
    }
}

if you don't want to connect to the current domain and instead want to connect to Domain.com then replace the null with the appropriate string.

EDIT: if you want to use secure strings you can't use System.DirectoryServices.AccountManagement.PrincipalContext, you will need to go with the lower level calls in System.DirectoryServices.Protocols. Doing this process is quite complex, here is a link to the MSDN article "Introduction to System.DirectoryServices.Protocols (S.DS.P)" explaining how to use it. It is a big complex read and honestly I don't think it is worth it to be able to use encrypted strings.

public void PerformSomeActionAsAdmin(NetworkCredential adminCredential)
{
    using(LdapConnection connection = new LdapConnection("fabrikam.com", adminCredential))
    {
       // MAGIC
    }
}

Upvotes: 2

Do you want to check if the current user is a doman admin? start by looking at his code, it should help you get started identifying what AD groups the current user is in. This will give you a list of strings that are each group's name the current user belongs to. Then you can check that list against whatever AD group you are trying to check for. Replace YourDomain with your domain name:

WindowsIdentity wi = WindowIdentity.GetCurrent();

List<string> result = new List<string>();

foreach (IdentityReference group in wi.Groups)
                    {
                        result.Add(group.Translate(typeof(NTAccount)).ToString().Replace("YourDomain\\", String.Empty));
                    }

Since i'm not quite sure what you're trying to do, this also might be helpful. You'd have to get the user name and password from a textobx, password box etc. This could be used for an "override" to use, for example, a manager's credentials etc. to do something the current user wasn't allowed to do because of AD group membership etc.

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YourDomain"))
                {
                    if (UserName.Contains("YourDomain\\"))
                    {
                        UserName = UserName.Replace("YourDomain\\", String.Empty);
                    }
                    //validate the credentials
                   bool IsValid = pc.ValidateCredentials(UserName, Password);
                }

Upvotes: 0

Related Questions