Solved

How to Resolve ASP.NET C# 4.5 "Unloaded Appdomain Error"

Posted on 2013-10-31
4
4,243 Views
Last Modified: 2014-01-13
I am working with LDAP, We have Authentication Complete, We are inside the Domain on a webserver.

This error occurs after I inserted code that performs a match with an AD role.  The objective was to get the role from AD then mimic its presence in ASP.NET by "placing" the user in a role within the web application.    There is no standard membership or SQL DB... just the remote AD SQL DB.

Unloaded Appdomain Error
using SavvyLDAP;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated != true)
        {
            Response.Redirect("~/Login.aspx");
        }
        else
        {
            lbluName.Text = HttpContext.Current.User.Identity.Name.ToString();
            string username = HttpContext.Current.User.Identity.Name.ToString();
            string domain = "dpi";
            string domainAndUsername = domain + @"\" + username;
            GetFilteredRoles(domainAndUsername);
        }
    }

    #region Return All AD Groups - Filtered

    private List<GroupPrincipal> GetFilteredRoles(string username)
    {
        //string domain = "dpi";
        //string domainAndUsername = domain + @"\" + username;
        List<GroupPrincipal> ADFresult = new List<GroupPrincipal>();
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
        if (user != null)
        {
            PrincipalSearchResult<Principal> ADFgroups = user.GetAuthorizationGroups();
// the codes below are the ones causing the Unloaded AppDomain issue
            Principal DMPDBRole = ADFgroups.FirstOrDefault(x => x.Name.Contains("DMPDB_"));
            Session.Add("Role", DMPDBRole);
            TransferRole();
        }     
        return ADFresult;        
    }
    #endregion 

    protected void TransferRole()
    {
        if (Session["Role"].ToString() == "DMPDB_Admin")
        {
             // the role insertion code throws a separate Network Service Error
            //Roles.AddUserToRole(username, "DMPDB_Admin");
            Response.Redirect("App_Roles/Admin/Default.aspx");
        }
        else if (Session["Role"].ToString() == "DMPDB_Owner")
        {
            //Roles.AddUserToRole(username, "DMPDB_Owner");
            Response.Redirect("App_Roles/Owner/Default.aspx");
        }
        else if (Session["Role"].ToString() == "DMPDB_Editor")
        {
            //Roles.AddUserToRole(username, "DMPDB_Editor");
            Response.Redirect("App_Roles/Editor/Default.aspx");
        }
        else if (Session["Role"].ToString() == "DMPDB_Reader")
        {
            //Roles.AddUserToRole(username, "DMPDB_Reader");
            Response.Redirect("App_Roles/Reader/Default.aspx");
        }
        else
        {
            Response.Redirect("App_Roles/Unauthorised.aspx");
        }
    }
}

Open in new window

0
Comment
Question by:XGIS
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 500 total points
ID: 39620028
stab in the dark, but you want to make sure your web config is tight - make sure that the LDAP connections are setup correctly - just google LDAP asp.etn authenitication examples.

principles, identity and contexts generally shape around threads, when the app runs under th app domain, this thread needs to the relevant settings and config to access your ldap source, so I would aim some sort of config issue.

quick google though and there is a bug in 4.0 that might need a hotfix (though you would suspect 4.5 shouldnt need this? urggg...

stackoverflow.com/questions/7168361/determine-if-user-is-in-ad-group-for-net-4-0-application/13594279#13594279
0
 
LVL 7

Author Comment

by:XGIS
ID: 39652180
Hello deanvanrooyen.. thankyou for the feedback.. I will look into this ASAP.
0
 
LVL 7

Author Closing Comment

by:XGIS
ID: 39778521
Thankyou for your time and patience and input ... the problem was partly related to configuration and incorrectly structured code logic.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question