简体   繁体   English

在C#上从Active Directory获取全名

[英]Get fullname from Active Directory on C#

I have a problem about getting user's firstname and surname (fullname) from Active Directory. 我有一个关于从Active Directory获取用户的名字和姓氏(全名)的问题。 The code is below. 代码如下。

No problem with runing the project on my local. 在本地运行项目没有问题。 I can take data whatever I want. 我可以随心所欲地获取数据。 After publishing the project to remote server, it don't allow me to get firstname and surname (fullname), it just allows to get domain name and username. 将项目发布到远程服务器后,它不允许我获取名字和姓氏(全名),而仅允许获取域名和用户名。 How can I fix it? 我该如何解决?

private string GetNameSurname()
    {
        string[] retVal;
        //Pull the username out of the domain\user string.  
        retVal = Page.User.Identity.Name.Split(new string[] { @"\"},StringSplitOptions.RemoveEmptyEntries);

        DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + retVal[0] + "/" + retVal[1]);
        string namesurname = (string)userEntry.Properties["fullname"].Value;
        //return retVal[1];
        //retVal[1] gives username. 
        //retVal[0] gives domain name
        return namesurname;
    }

Are you running the code to test for the same user? 您是否正在运行代码以测试同一用户?

Are you testing against AD in dev, Windows local accounts have a Full Name field (optional) but in AD the fields are named differently. 您是否要在开发人员中针对AD进行测试,Windows本地帐户具有“全名”字段(可选),但是在AD中,这些字段的名称不同。 Also, are you talking to the same version of AD. 另外,您是否在谈论相同版本的AD。

According to the MSDN article there is no field called Full Name, only First Name and Surname and Display Name. 根据MSDN文章 ,没有名为“全名”的字段,只有“名字,姓氏和显示名”。

I think your problem is about credentials. 我认为您的问题与凭证有关。 Try to add an administrator username and password when conecting to active directory. 连接到活动目录时,尝试添加管理员用户名和密码。

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

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