Comparing SPPrincipal Objects To See If Same Member

More because I needed to recall this method that anything. Simple helper method consuming two SPPrincpal objects to compare the membership. Simple property and type testing, nothing very fancy.

[csharp]

public static bool SameMember(SPPrincipal x, SPPrincipal y)

{

if ((x is SPGroup) && (y is SPGroup))

{

var leftGroup = x as SPGroup;

var rightGroup = y as SPGroup;

return string.Equals(leftGroup.Name, rightGroup.Name, StringComparison.CurrentCultureIgnoreCase);

}

if (!(x is SPUser) || !(y is SPUser))

{

return false;

}

var leftUser = x as SPUser;

var rightUser = y as SPUser;

if ((!leftUser.IsDomainGroup || !rightUser.IsDomainGroup) && (leftUser.IsDomainGroup || rightUser.IsDomainGroup))

{

return false;

}

return string.Equals(leftUser.LoginName, rightUser.LoginName, StringComparison.CurrentCultureIgnoreCase);

}

[/csharp]

Share

One Comment

  1. Vishnu says:

    Pretty good SharePoint security tip!

Leave a Reply

Your email address will not be published. Required fields are marked *