【问题标题】:Fetching full name from Active Directory using C# doesn't work after deployment部署后使用 C# 从 Active Directory 获取全名不起作用
【发布时间】:2015-11-29 15:07:00
【问题描述】:

我正在使用以下代码从服务器获取用户的全名:

public string getUserName(int empID)
        {

            DirectoryEntry objDirectoryEntry = new DirectoryEntry("LDAP://SOMEDOMAIN.com");
            objDirectoryEntry.AuthenticationType = AuthenticationTypes.Secure;

            DirectorySearcher objDirectorySearch = new DirectorySearcher(objDirectoryEntry);
            objDirectorySearch.Filter = "(SAMAccountName=" + empID + ")";
            objDirectorySearch.PropertiesToLoad.Add("displayName");

            SearchResult objSearchResult = objDirectorySearch.FindOne();

            if (objSearchResult != null)

                return Convert.ToString(objSearchResult.Properties["displayname"][0]);

            else

                return "User not found";

        }

这在我的本地机器上运行良好。

但是,将其部署到服务器后,它根本不起作用。

这将返回 NULL。

正如@marc_s 建议的那样,我尝试使用以下代码:

public string getUserStackExchange(string empID)
        {
            string fullName = empID;
            // set up domain context using the default domain you're currently logged in 
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
            {
                // find a user
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, empID);

                /* or if you're interested in the *currently logged in* user,
                   then you could also use:
                UserPrincipal user = UserPrincipal.Current;
                */

                if (user != null)
                {
                    // get the "DisplayName" property ("Fullname" is WinNT specific)
                    fullName = user.DisplayName;

                    // do something here....        
                }
            }
            return fullName;
        }

同样的结果。在本地工作得很好。但是部署后就不行了。 但是,如果我在调试模式下运行项目并打开断点,它会完美地获取数据。

代码或 IIS 配置有问题吗(我怀疑这个)?

【问题讨论】:

  • 学习记录你正在做的事情并调试你自己的代码。
  • WinNT:// 提供程序用于您的本地计算机用户存储 - 它不是 Active Directory - 基于网络的存储!您需要为此使用LDAP:// 提供程序!
  • @marc_s 谢谢.. 我试过使用 LDAP。结果是一样的。在本地工作正常。在服务器上不起作用。请检查更新的问题。
  • 该属性称为displayName(不是displayname - 是的,我认为它实际上是区分大小写) - 尝试使用正确的拼写 - 是否返回值?或者也许该值只是没有在该用户帐户上设置...

标签: c# asp.net iis active-directory ldap


【解决方案1】:

如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context using the default domain you're currently logged in 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Environment.UserName);

    /* or if you're interested in the *currently logged in* user,
       then you could also use:
    UserPrincipal user = UserPrincipal.Current;
    */

    if(user != null)
    {
        // get the "DisplayName" property ("Fullname" is WinNT specific)
        string fullName = user.DisplayName; 

        // do something here....        
    }
}

新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

【讨论】:

  • 我试过用它。同样的事情也发生在这里。它在本地运行良好。但部署后它不起作用。但是,如果我在调试模式下运行项目并打开断点。它工作正常。我怀疑 IIS 有问题。你怎么看@marc_s?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多