D
D
des1roer2016-04-04 07:31:34
ASP.NET
des1roer, 2016-04-04 07:31:34

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

1 answer(s)
S
Sergey, 2016-04-04
@des1roer

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 question

Ask a Question

731 491 924 answers to any question