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

How To Use PolicyLevel Objects For Display And Debugging

PolicyLevel objects are great when you are working with CLR level security since there are both several useful properties, as well as all being documented. Coming from the SharePoint OM world that is a breath of fresh air.

Working with PolicyLevel objects is pretty straight forward, as the name implies these objects represent security policy levels for the runtime, used to compute the permission grant. As such, for things like querying assemblies for permission correlation, or permissions set’s directly, PolicyLevel‘s let you do all that sort of fun stuff!

Consider the below example. Firstly, I am going to drill down and get all assemblies that have full trust using the FullTrustAssemblies property.

[csharp]

IEnumerator enumerator = myPolicy.FullTrustAssemblies.GetEnumerator();
for (int i = 1; enumerator.MoveNext(); i++)
{
var current = (StrongNameMembershipCondition)enumerator.Current;
var builder = new StringBuilder();
builder.AppendFormat(“{0} | “, i);
if (current.Name != null)
{
builder.Append(current.Name);
}
}
[/csharp]
Next, I am going to use the NamedPermissionSets property, loop through it and display the policy levels permission sets.
[csharp]
IEnumerator enumerator = level.NamedPermissionSets.GetEnumerator();
for (var i = 1; enumerator.MoveNext(); i++)
{
var current = (NamedPermissionSet)enumerator.Current;
var builder = new StringBuilder();
builder.AppendFormat(“{0} | {1}”, i, current.Name);
}
[/csharp]
Lots of possibilities.

Share