Solved

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

Posted on 2013-10-31
4
4,220 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

Are your AD admin tools letting you down?

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

Suggested Solutions

Title # Comments Views Activity
Office 2016 Network Activation 2 13
Icons and Colors for Terms 3 24
Get id from json Data with NewtonSoft 3 31
VB.Net How to Exit Sub - Exit Form??? 5 47
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now