我要如何找到在C#Web应用程序用户的Active Directory显示名称?我要、应用程序、名称、用户

2023-09-08 12:30:10 作者:ー個亽遠行

我正在写一个Web应用程序,它使用Windows身份验证,我可以使用类似愉快地获取用户的登录名:

I'm writing a web application which uses windows authentication and I can happily get the user's login name using something like:

 string login = User.Identity.Name.ToString();

但我并不需要他们的登录名我希望自己的显示名称。我一直在敲打我的头一两个小时了......

But I don't need their login name I want their DisplayName. I've been banging my head for a couple hours now...

我可以通过一个Web应用程序访问我的组织的AD?

Can I access my organisation's AD via a web application?

推荐答案

这个怎么样:

private static string GetFullName()
    {
        try
        {
            DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
            return de.Properties["fullName"].Value.ToString();
        }
        catch { return null; }
    }