M4V3R1CK
M4V3R1CK

Reputation: 771

MVC 3 Not Recognizing Windows Role (Group)?

Has anybody come across occurrences where MVC does not recognize roles from Windows? I thought that roles translated to groups in Windows, but for some reason when I add a user to a group, check in MVC (using windows authentication) if that user.IsInRole("GroupJustAddedTo") always returns false. I have no idea why....Working in Server 2003 R2 with Windows Authentication enabled around the board. Confused :~( ???

Upvotes: 0

Views: 508

Answers (1)

Nick DeVore
Nick DeVore

Reputation: 10166

Without knowing anything else, it makes me wonder if perhaps your MVC is not really connected to your AD server. Also, perhaps the user that is fetching the groups doesn't have sufficient privileges? Just some initial thoughts.

EDIT

We ended up writing our own RoleProvider. Here is the overloaded GetRolesForUser code

public override string[] GetRolesForUser(string userName)
{
    List<string> allRoles = new List<string>();
    PrincipalContext context;
    context = new PrincipalContext(ContextType.Domain, "hlpusd.k12.ca.us", "DC=hlpusd,DC=k12,DC=ca,DC=us");
    UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);
    PrincipalSearchResult<Principal> usergroups = user.GetGroups(); // list of AD groups the user is member of
    IEnumerator<Principal> eGroup = usergroups.GetEnumerator();
    while (eGroup.MoveNext())
    {
        allRoles.Add(eGroup.Current.Name);
    }

    return allRoles.ToArray();
}

Upvotes: 1

Related Questions