Answer the question
In order to leave comments, you need to log in
Asp net find out group membership?
Can you suggest code to check if a user is in a specific group?
this code doesn't work
static bool IsUserMemberOf(string userName, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain))
using (var groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName))
using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, userName))
{
return userPrincipal.IsMemberOf(groupPrincipal);
}
}
Answer the question
In order to leave comments, you need to log in
If Microsoft.AspNet.Identity is used then User.IsInRole("Admin") for the current user. If necessary for anyone, then in the case of Microsoft.AspNet.Identity.EntityFramework:
var context = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(context);
var roleMgr = new RoleManager<IdentityRole>(roleStore);
roleMgr.FindByName("Admin").Users - даёт список пользователей.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question