[英]Getting current login from Active Directory using C# code
如何使用 C# 代码从 Windows Active Directory 获取当前用户的登录名?
Simply,简单地,
string Name = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;
OR要么
string Name = System.Environment.UserName
OR要么
string Name = Environment.GetEnvironmentVariable("USERNAME");
OR要么
string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
works :)作品:)
If you're on .NET 3.5 and up, you can use:如果您使用的是 .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"
}
The new S.DS.AM makes it really easy to play around with users and groups in AD!新的 S.DS.AM 使在 AD 中与用户和组一起玩变得非常容易!
References:参考:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.Name
This is also working for me!这也对我有用! Thanks
谢谢
我得到了“NT AUTHORITY\\NETWORK SERVICE”以及其他提供的解决方案,但System.Threading.Thread.CurrentPrincipal.Identity.Name.ToString()为我工作。
我有这个想法,非常适合我!
<h5 class="mb-0 text-gray-800">Welcome, <span style="text-transform:capitalize">@User.Identity.Name.Replace("AD-GROUP-NAME\\\\", "").Replace(".", " ")</span></h5>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.