【问题标题】:Why my Code is working on the localhost but not on a web server with IIS7?为什么我的代码在 localhost 上运行,而不是在带有 IIS7 的 Web 服务器上运行?
【发布时间】:2013-10-24 12:56:45
【问题描述】:

您好,我的 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?

【问题讨论】:

  • 啊是的老Der Objektverweis错误
  • P 或 u 上的 NullReference?
  • 听起来不像是 IIS 问题...听起来 p 为空。
  • 评论者。如果您理解错误,请将其翻译成英文。

标签: c# asp.net iis iis-7 webserver


【解决方案1】:

尝试将 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);
        }
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2012-11-07
    • 2020-04-21
    • 2011-04-24
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多