public static bool UserIsMemberOfGroups(string username, string[] groups) { /* Return true immediately if the authorization is not * locked down to any particular AD group */ if (groups == null || groups.Length == 0) { return(true); } CompXDbContext db = new CompXDbContext(); UserAccount user = db.UserAccounts.Find(HttpContext.Current.User.Identity.Name.ToLower()); if (user == null) { return(false); } foreach (var group in groups) { if (user.permissionGroup == group) { return(true); } } return(false); //OLD FROM ACTIVE DIRECTORY // Verify that the user is in the given AD group (if any)x /* string connString = ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString; * int startIndex = connString.IndexOf("//") + 2; * string domain = connString.Substring(startIndex, connString.IndexOf(".", startIndex) - startIndex); * var context = new PrincipalContext(ContextType.Domain, domain); * var userPrincipal = UserPrincipal.FindByIdentity(context, * IdentityType.SamAccountName, * username); * * foreach (var group in groups) * { * if (userPrincipal.IsMemberOf(context, IdentityType.Name, group)) * { * return true; * } * } * * return false;*/ }
public static bool UserIsMemberOfGroupOC(string group, string userName) { /* Return true immediately if the authorization is not * locked down to any particular AD group */ if (String.IsNullOrWhiteSpace(group)) { return(true); } CompXDbContext db = new CompXDbContext(); UserAccount user = db.UserAccounts.Find(userName.ToLower()); if (user == null) { return(false); } if (user.permissionGroup == group) { return(true); } return(false); }