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