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

LVL 7
XGISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
something like this:
using System.Linq;
...
string[] groupsToCheckAgainst = { "DM_Admin", "DM_Owner", "DM_Editor", "DM_Reader" };

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
			if (groupsToCheckAgainst.Contains(principal.Name))
			{
				Session["Group"] =  principal.Name;
				break;
			}
		}
	}
}

Open in new window


If you want to check each and every group, I would suggest to create 4 session properties of type "boolean" and set it to true if the user has this role/is in this group like
using System.Linq;
...
string[] groupsToCheckAgainst = { "DM_Admin", "DM_Owner", "DM_Editor", "DM_Reader" };

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

	if (user != null)
	{
		Session["DM_Admin"] = false;
		Session["DM_Owner"] = false;
		Session["DM_Editor"] = false;
		Session["DM_Reader"] = false;
		
		// 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
			if (groupsToCheckAgainst.Contains(principal.Name))
			{
				Session[principal.Name] =  true;
			}
		}
	}
}

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XGISAuthor Commented:
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
0
XGISAuthor Commented:
Hello Rainer..I am still awaiting feedback from developers. I will give you an update ASAP.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

XGISAuthor Commented:
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

0
Rainer JeschorCommented:
Hi,
can you check if Session is enabled in web.config?
0
XGISAuthor Commented:
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

0
XGISAuthor Commented:
Helped put us on the right track...  Thankyou for your time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.