Link to home
Start Free TrialLog in
Avatar of XGIS
XGISFlag for Australia

asked on

Match AD Groups in ASP.NET C# Class and Display Value in ASPX

I am using a snippet of Code below in a C# Class (ASP.NET 4.5).  
I need to insert some code to help me filter existing roles against 4 separate roles.
The users are on a domain and already have multiple AD roles/groups.  
I have four static groups that I need to filter from other user roles.
They are;  DM_Admin, DM_Owner, DM_Editor, DM_Reader

The code section I need to populate  is "// do something with the group (or role) in question"

The code is working, the users are authenticated from the class code not displayed here.

I then need to store that data in Session so I can display it on my ASPX Page.  
Class: eg //Session["Group"] =  group;
ASPX: lblGroup.Text = Session["Group"].ToString();

a bonus would be how to deal with a user that is in one or more of the 4 roles, but I am only seeking a single match at this stage

 using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
            {
                // find a user
                UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);

                if (user != null)
                {
                    // get the authorization groups - those are the "roles" 
                    var groups = user.GetAuthorizationGroups();

                    foreach (Principal principal in groups)
                    {
                        // do something with the group (or role) in question
                    }
                }
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XGIS

ASKER

Hello Rainer.. thankyou for supplying those codes and logic... We will run some tests.  I will get back to you in 36 hours with feedback on our level of success Cheers Aaron
Avatar of XGIS

ASKER

Hello Rainer..I am still awaiting feedback from developers. I will give you an update ASAP.
Avatar of XGIS

ASKER

Hello Rainer...  Pls note I am back on the code task... I know this is lame but I am having issues getting the session to the default.aspx from the class. It throws an object reference error. I even setup a class to hold the session variables but no joy there either.  We will keep trying though.  

LDAP Class
 //Session.Add["Group"] = principal.Name;
//Session["Group"] = principal.Name;

ASPX
lbluGroup.Text = Session["Group"].ToString();

Session Class'
using System.Web;

public static class Geek
{

    #region DMPDB Sessions

    private static string _dmpdbKey = "Group";

    #endregion

    #region DMPDB Session Declarations

    public static object Group { get { if (HttpContext.Current.Session[Geek._dmpdbKey] == null) { return string.Empty; } else { return HttpContext.Current.Session[Geek._dmpdbKey].ToString(); } } set { HttpContext.Current.Session[Geek._dmpdbKey] = value; } }

    #endregion
}

Open in new window

Hi,
can you check if Session is enabled in web.config?
Avatar of XGIS

ASKER

Hello Rainer...We ended up removing the for each and wrote a role class and brought the C# to the Default.aspx

            
Principal grp = ADFgroups.FirstOrDefault(x => x.Name.Contains("DMPDB_"));
 lbluGroup.Text = grp.ToString();

Open in new window

Avatar of XGIS

ASKER

Helped put us on the right track...  Thankyou for your time.