Solved

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

Posted on 2013-10-31
4
4,198 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
Comment Utility
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
Comment Utility
Hello deanvanrooyen.. thankyou for the feedback.. I will look into this ASAP.
0
 
LVL 7

Author Closing Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

744 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

17 Experts available now in Live!

Get 1:1 Help Now