为什么我的代码在本地主机上工作,但在具有 IIS7 的 Web 服务器上不起作用

本文关键字:IIS7 Web 不起作用 服务器 工作 代码 我的 为什么 主机 | 更新日期: 2023-09-27 18:36:53

嗨,我的 asp.net 应用程序有问题。

问题是我可以在我的本地主机上毫无问题地执行我的应用程序,但是如果我在服务器上的IIS7中安装它,则会出现错误。我尝试查找错误,并在一个区域中选择了错误。

以下是错误消息:

Object reference not set to an instance of an object.
bei linde_wiemann_gastzugang.linde_wiemann_daten.IsGroupMember(String dc, String user, String group) in D:'Programmierung'Visual_Studio_2010'Projekte'lw_gastzugang'lw_gastzugang'lw_daten.cs:Zeile 30. 

这是代码:

public static bool IsGroupMember(string dc, string user, string group)
{
    try
    {
        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc))
        {
            bool found = false;
            GroupPrincipal p = GroupPrincipal.FindByIdentity(ctx, group);
            UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user);
            found = p.GetMembers(true).Contains(u); //I think the error is here :(
            p.Dispose();
            u.Dispose();
            return found; // <-- Zeile 30
        }
    }
    catch (Exception ex)
    {
        EventLogManager.CreateEventLog("Gastzugang",ex.Message + " : " + dc + " - " + user + " - " + group);
        return false;
    }

我尝试使用硬编码值如何真实,并且它适用于它们:/是什么让我不能使用此代码的 IIS?

为什么我的代码在本地主机上工作,但在具有 IIS7 的 Web 服务器上不起作用

尝试将 p 和 u 放入一个 using 子句中:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc))
{
    using (GroupPrincipal p = GroupPrincipal.FindByIdentity(ctx, group))
    {
        using (UserPrincipal u = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user))
        {
            return p.GetMembers(true).Contains(u);
        }
    }
}

我认为您遇到了处置问题。