?
Solved

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

Posted on 2013-10-31
4
Medium Priority
?
4,359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 2000 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

777 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