C# .NET - Getting User Information in Domain
This article provides an example of getting user information in a domain AD using C#.
To make it work, you need to add the library:
using System.DirectoryServices.AccountManagement;
Then you can get user parameters.
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
var loginName = HttpContext.User.Identity.Name.ToString();
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, loginName);
if (user != null)
{
// Получаем SID пользователя
var userSid = user.Sid;
// Получаем EMail пользователя
var userEmail = user.EmailAddress;
// Получаем Количество неудачных попыток входа пользователя
var userBadLogons = user.BadLogonCount;
// ФИО пользователя ( отображаемое имя)
var userName = user.DisplayName;
// Системное имя (по сути логин)
var userSamAccountName = user.SamAccountName;
var userFullName = String.Format("{0} {1} {2}", userName, userSamAccountName, userBadLogons);
ViewBag.Message = userFullName;
}
The loginName variable in this example gets the domain user's login from HttpContext.
The provided code is for ASP.NET MVC.
However, nothing prevents you from using it in any other type of application, just pass the domain user's name in the format: 'domain\login'.
Comments (0)
No comments yet.