Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-10-31
4
Medium Priority
?
4,391 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
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 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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses

618 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