LinqToLdap,在localhost工作正常,但主机在IIS服务器返回错误

时间:2013-09-03 06:38:16

标签: sql-server-2008 asp.net-mvc-4 ldap

奇怪的是,在localhost托管时,我可以从服务器获取AD列表,但是当服务器2008 R2上的主机,它返回错误,我使用LinqToLDAP,任何想法哪里出错?

            var config = new LdapConfiguration();
            config.ConfigureFactory(Server).ProtocolVersion(3);
            config.MaxPageSizeIs(Size);

            using (HostingEnvironment.Impersonate()) //solved issue for firefox and safari
            {
                using (var context = new DirectoryContext(config))
                {
                    context.Log = Console.Out;
                    var user = context.Query<ActiveDirectory>();

                    var result = (from m in user
                                  select m).ToList();
                    return result;
                }
            }

enter image description here

2 个答案:

答案 0 :(得分:0)

好的,找到答案后,通过谷歌搜索并玩了一下代码,弄清楚我需要通过网络凭证才能使其正常工作

答案 1 :(得分:0)

这是我当前系统中运行良好的代码,随时可以通过

进行学习

<强> WebConfig

<add key="LdapServer" value="yourADserverName" />
<add key="LdapSize" value="totalpagesziereturn" />
<add key="CreName" value="yourdomainuserid" />
<add key="CrePass" value="yourpassword" />
<add key="CreDomain" value="yourdomain" />

<强> AD.cs

    private int Size = Convert.ToInt32(ConfigurationManager.AppSettings["LdapSize"]);
    private string Server = ConfigurationManager.AppSettings["LdapServer"];
    private NetworkCredential UserCredential = new NetworkCredential(ConfigurationManager.AppSettings["CreName"], ConfigurationManager.AppSettings["CrePass"], ConfigurationManager.AppSettings["CreDomain"]);

    /// <summary>
    /// Get all user from active directory
    /// </summary>
    /// <returns></returns>
    public IEnumerable<ActiveDirectory> GetActiveDirectory()
    {
        var config = new LdapConfiguration();
        config.ConfigureFactory(Server).ProtocolVersion(3).UsePort(389).AuthenticateAs(UserCredential);
        config.MaxPageSizeIs(Size);

        using (HostingEnvironment.Impersonate()) //solved issue for firefox and safari
        {
            using (var context = new DirectoryContext(config))
            {
                context.Log = Console.Out;
                var user = context.Query<ActiveDirectory>();

                var result = (from m in user
                              select m).ToList();
                return result;
            }
        }
    }