Подтвердить что ты не робот

Получение текущего входа в Active Directory с помощью кода С#

Как получить текущее имя пользователя из Windows Active Directory с помощью кода С#?

4b9b3361

Ответ 1

Просто,

string Name = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;

ИЛИ

string Name = System.Environment.UserName  

ИЛИ

string Name = Environment.GetEnvironmentVariable("USERNAME");

ИЛИ

string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

работает:)

Ответ 2

Если вы используете .NET 3.5 и выше, вы можете использовать:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find current user
UserPrincipal user = UserPrincipal.Current;

if(user != null)
{
   string loginName = user.SamAccountName; // or whatever you mean by "login name"
}    

Новый S.DS.AM позволяет очень легко играть с пользователями и группами в AD!

Литература:

Ответ 3

System.DirectoryServices.AccountManagement.UserPrincipal.Current.Name

Это тоже работает для меня! Благодаря

Ответ 4

Я получал "NT AUTHORITY\NETWORK SERVICE" с другими предлагаемыми решениями, но System.Threading.Thread.CurrentPrincipal.Identity.Name.ToString() работал для меня.