BlogEngine.net and Active Directory membership provider woes

We wanted our blog to use the user credentials found in our company's Active Directory, so after a little bit of web surfing we found a great little extension here. After going through the steps needed to install this extension we launched our webbrowser to test our updated blog...

Error: 'Unable to cast object of type 'System.Security.Principal.SecurityIdentifier' to type 'System.Security.Principal.NTAccount'

After some debugging we found out it had something to do with the GetRolesForUser() function found in the file ADRoleProvider.cs. Apperently something goes wrong when translating the token groups to the NTAccount-type. Knowing the cause of the problem, the solution was easy. Replace the foreach loop in the GetRolesForUser() function with:

foreach (IdentityReference group in groupCollection)
{
    if (group.Value.IndexOf(@"\") == -1)
    {
        // Skip groups not containing backslash
        continue;
    }

    String roleName = RGConfig.getRole(group.Value.Substring(group.Value.IndexOf(@"\") + 1));

    if (roleName != String.Empty)
    {
        roles.Add(roleName);
    }
}

After this little tweak the extension from rafekemmis worked!

Download: ActiveDirectoryRoleProvider.zip (5.39 kb)

FacebookDigg It!NewsVineRedditStumbleUpon

Tags: , , , , ,