Home
Manage Your Code
Snippet: Is User in Role (Group) (C#)
Title: Is User in Role (Group) Language: C#
Description: Test to see if a user is in a domain role. Views: 307
Author: Kevin Shuma Date Added: 11/27/2007
Copy Code  
1/// <summary>

2		/// See if a specified user is in a role(s).

3		/// </summary>

4		/// <param name="DomainName">string.  The user's domain name.  Example "AMERICAS"</param>

5		/// <param name="UserName">string.  Active Directory user name.  Do not include the domain.  Example: "kevin_shuma"</param>

6		/// <param name="RoleNames">string array.  List of Active Directory roles.</param>

7		/// <returns>True = User is in one of the roles specified, False = User is not in any of the roles specified.</returns>

8		public static bool UserInRole(string DomainName, string UserName, string[] RoleNames)
9		{
10			try
11			{
12				bool InRole = false;
13				string[] Roles = RoleMemberships(DomainName, UserName);//, out Error);

14				if (Roles != null)
15				{
16					for(int i = 0; i < Roles.Length; i++)
17					{
18						for (int r = 0; r < RoleNames.Length; r++)
19						{
20							if (Roles[i].ToUpper().CompareTo(RoleNames[r].ToUpper()) == 0)
21							{
22								InRole = true;
23								break;
24							}
25						}
26						if (InRole)
27							break;
28					}
29				}
30				//Error = null;

31				return InRole;
32			}
33			catch (Exception ex)
34			{
35				//Error = ex;

36				Logging.LogException(MethodBase.GetCurrentMethod(), ref ex);
37				return false;
38			}
39		}
Notes
Comment out custom exception handling before using.