繁体   English   中英

如何获得已登录Windows用户的名字和姓氏?

[英]How do I get the first name and last name of the logged in Windows user?

如何在系统中使用c#获取我的姓氏(使用Active Directory用户名登录并通过Windows)?

是否可以不通过广告就这样做?

如果您使用的是.Net 3.0或更高版本,则有一个可爱的库可以使它自己编写。 System.DirectoryServices.AccountManagement具有一个UserPrincipal对象,该对象可以准确获取您要查找的内容,而您无需弄乱LDAP或直接进行系统调用即可。 这就是全部:

Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
// or, if you're in Asp.Net with windows authentication you can use:
// WindowsPrincipal principal = (WindowsPrincipal)User;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
    return up.DisplayName;
    // or return up.GivenName + " " + up.Surname;
}

注意:如果已经有了用户名,则实际上不需要主体,但是如果在用户上下文中运行,从那里拉取它就很容易。

有一个更简单的方法可以做到这一点:

using System.DirectoryServices.AccountManagement;

UserPrincipal userPrincipal = UserPrincipal.Current;
String name = userPrincipal.DisplayName;

在此处输入图片说明

这个解决方案对我不起作用,但是这个功能很好用:

public static string GetUserFullName(string domain, string userName)
        {
            DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User");
            return (string)userEntry.Properties["fullname"].Value;
        }

您应该这样称呼:

GetUserFullName(Environment.UserDomainName, Environment.UserName);

在这里找到)。

批准的答案的问题在于,如果您具有“姓氏,名字”策略,则DisplayName会给Smith,John而不是John Smith。 有两种获取正确格式的方法,userPrincipal.Name属性包含“ John Smith(jsmith1)”,因此您可以使用它,而只是将string.Split放在“(”上。或者使用以下方法:

private string ConvertUserNameToDisplayName(string currentSentencedByUsername)
    {
        string name = "";
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            var usr = UserPrincipal.FindByIdentity(context, currentSentencedByUsername);
            if (usr != null)
                name = usr.GivenName+" "+usr.Surname;
        }
        if (name == "")
            throw new Exception("The UserId is not present in Active Directory");
        return name;
    }

这将提供原始海报所需的“ John Smith”表格。

最快的方法是使用已经拥有的SID直接绑定到其AD对象。 例如:

//ASP.NET
var identity = (WindowsIdentity) HttpContext.Current.User.Identity;

//Desktop
var identity = WindowsIdentity.GetCurrent();

var user = new DirectoryEntry($"LDAP://<SID={identity.User.Value}>");

//Ask for only the attributes you want to read.
//If you omit this, it will end up getting every attribute with a value,
//which is unnecessary.
user.RefreshCache(new [] { "givenName", "sn" });

var firstName = user.Properties["givenName"].Value;
var lastName = user.Properties["sn"].Value;

有同样的问题。 经过研究并浏览了Microsoft Docs,我找到了解决方案。

首先使用Nuget软件包管理器安装System.DirectoryServices.AccountManagement软件包。

然后,在调用GetUserNameWithoutDomain方法时,将以下内容作为参数传递:

GetUserNameWithoutDomain(UserPrincipal.Current.GivenName, UserPrincipal.Current.Surname); 这绝对应该工作!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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