简体   繁体   中英

Get domain UserName for computer that not in domain

I hope somebody could help me to solve my problem.

There is some domain, and some shared directory in that domain. User has domain username and password, to get access to that shared directory, but his computer is not in domain most of time. I mean, when he need to connect to that directory he is typing network path (for example "\\\\fileserver"), than windows asks for username and password, and after writing it he will get the access until restart windows or logout.

So my question is, how can i get domain user name that user uses to connect to network, by using C# code?

I tryed to use CredentialCache.GetCredential, but i think it works only if user connect to shared directory by using NetworkCredentials in C# program. If connection already was before C# program started, it will be empty. I also tryed Environment.UserName, but since computer is not in domain, it returns only local user name, that different from domain username.

Sorry for my bad english, i hope you could understand my explanation.

Have you took a look at the nugget package CredentialManagement ?

I used it already, but couldn't roam credential saved with enterprise persistence

using CredentialManagement;

using (CredentialSet credentials = new CredentialSet())
{
    Debug.Print(credentials.Count.ToString() + " credential(s) found.");
    foreach (Credential credential in credentials)
    {
        Debug.Print(credential.Target); //This is domain/IP of the saved credential.
        Debug.Print(credential.Type.ToString());
        Debug.Print(credential.Username);
        Debug.Print(credential.PersistanceType.ToString());
        Debug.Print(credential.Description);
        Debug.Print(credential.LastWriteTime.ToString());
    }
    Debug.Print("End");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM