【问题标题】:Not getting Active Directory details after hosting托管后未获取 Active Directory 详细信息
【发布时间】:2014-07-04 13:07:42
【问题描述】:

我编写了 C# 代码来从 Active Directory 获取电子邮件。它在我的本地系统上运行良好,但托管后我没有收到电子邮件地址。以下是我已经尝试过的东西-

  1. 将应用程序池标识更改为NetworkService
  2. 启用 Windows 和 Digest 身份验证(同时启用,也逐个启用)

代码:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "comppany.com" , "DC=compnay,DC=com", ContextOptions.Negotiate))
// tried above and below//(ContextType.Domain, System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName))
{
                // validate the credentials
                bool isValid = pc.ValidateCredentials(Uid, Pwd);

                if (isValid)
                {
                    try
                    {
                        using (UserPrincipal up = UserPrincipal.FindByIdentity(pc, Uid))
                        {
                            return up != null && !String.IsNullOrEmpty(up.EmailAddress) ? up.EmailAddress : string.Empty;
                        }
                        //return "Validate successfully.";
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }
}

也尝试过关注-

using (var connection = new DirectoryEntry())
{
      using (var search = new DirectorySearcher(connection)
      {
         Filter = "(samaccountname=" + Uid + ")",
          PropertiesToLoad = { "mail" },
       })
       {
         return (string)search.FindOne().Properties["mail"][0];
       }
}

在 IIS7.0 中托管应用程序后,它们都不工作

请帮忙。 谢谢

【问题讨论】:

  • 你试过远程调试吗?还是记录?

标签: c# iis-7 active-directory


【解决方案1】:

这将是因为您的用户(即您)将有权从 Active Directory 读取,但 IIS 用户和网络服务不会。

在 using 语句周围放置一个 try catch,您应该会在异常中看到这一点。

还有其他 PrincipalContext 构造函数允许您指定要连接的用户的详细信息,或者您可以更改 IIS 应用程序池以作为具有权限的用户运行 - 不过我会使用 PrincipalContext 方式。

作为一个快速测试尝试this PrincipalContext 构造函数的版本 - 将您的用户名和密码放入用户名和密码参数中,看看它在 IIS 中托管时是否有效 - 如果这有效,那么您需要想出一些方法通过配置传递用户详细信息。 (一般是只有读取权限的服务账号,密码不会经常更改)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-28
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多