Tarasov
Tarasov

Reputation: 3685

Why my Code is working on the localhost but not on a web server with IIS7?

hi I have a problem with my asp.net application.

The problem is that i can execute my application on my localhost without problems but if i install it in IIS7 on a server I get a error. I try to find the error and I have select the error in a area.

Here is the error message:

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. 

Here is the code:

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;
    }

I try to use a hardcode value how true and it works with them :/ What make the IIS that I don't can use this code?

Upvotes: 0

Views: 239

Answers (1)

SergejK
SergejK

Reputation: 109

try to put p and u into a using clause:

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);
        }
    }
}

i think you area running into a disposing issue.

Upvotes: 1

Related Questions