PrincipalPermission
Allows checks against the active principal (see IPrincipal) using the language constructs defined for both declarative and imperative security actions. This class cannot be inherited.
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading;
namespace System.Security.Permissions
{
[Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId = "SYSLIB0003", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public sealed class PrincipalPermission : IPermission, ISecurityEncodable, IUnrestrictedPermission
{
private IDRole[] _idArray;
public PrincipalPermission(PermissionState state)
{
switch (state) {
case PermissionState.Unrestricted:
_idArray = new IDRole[1] {
new IDRole(true, null, null)
};
break;
case PermissionState.None:
_idArray = new IDRole[1] {
new IDRole(false, string.Empty, string.Empty)
};
break;
default:
throw new ArgumentException(System.SR.Argument_InvalidPermissionState);
}
}
public PrincipalPermission(string name, string role)
{
_idArray = new IDRole[1] {
new IDRole(true, name, role)
};
}
public PrincipalPermission(string name, string role, bool isAuthenticated)
{
_idArray = new IDRole[1] {
new IDRole(isAuthenticated, name, role)
};
}
private PrincipalPermission(IDRole[] array)
{
_idArray = array;
}
private bool IsEmpty()
{
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
if (iDRole.ID == null || iDRole.ID.Length != 0 || iDRole.Role == null || iDRole.Role.Length != 0 || iDRole.Authenticated)
return false;
}
return true;
}
private bool VerifyType(IPermission perm)
{
if (perm != null)
return perm.GetType() == GetType();
return false;
}
public bool IsUnrestricted()
{
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
if (iDRole.ID != null || iDRole.Role != null || !iDRole.Authenticated)
return false;
}
return true;
}
public bool IsSubsetOf(IPermission target)
{
if (target == null)
return IsEmpty();
if (!VerifyType(target))
throw new ArgumentException(System.SR.Format(System.SR.Argument_WrongType, GetType().FullName), "target");
PrincipalPermission principalPermission = (PrincipalPermission)target;
if (principalPermission.IsUnrestricted())
return true;
if (IsUnrestricted())
return false;
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
bool flag = false;
IDRole[] idArray2 = principalPermission._idArray;
foreach (IDRole iDRole2 in idArray2) {
if (iDRole2.Authenticated == iDRole.Authenticated && (iDRole2.ID == null || (iDRole.ID != null && iDRole.ID.Equals(iDRole2.ID))) && (iDRole2.Role == null || (iDRole.Role != null && iDRole.Role.Equals(iDRole2.Role)))) {
flag = true;
break;
}
}
if (!flag)
return false;
}
return true;
}
public IPermission Intersect(IPermission target)
{
if (target == null)
return null;
if (!VerifyType(target))
throw new ArgumentException(System.SR.Format(System.SR.Argument_WrongType, GetType().FullName), "target");
if (IsUnrestricted())
return target.Copy();
PrincipalPermission principalPermission = (PrincipalPermission)target;
if (principalPermission.IsUnrestricted())
return Copy();
List<IDRole> list = null;
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
IDRole[] idArray2 = principalPermission._idArray;
foreach (IDRole iDRole2 in idArray2) {
if (iDRole2.Authenticated == iDRole.Authenticated) {
string id = string.Empty;
string role = string.Empty;
bool authenticated = iDRole2.Authenticated;
bool flag = false;
if (iDRole2.ID == null || iDRole.ID == null || iDRole.ID.Equals(iDRole2.ID)) {
id = (iDRole2.ID ?? iDRole.ID);
flag = true;
}
if (iDRole2.Role == null || iDRole.Role == null || iDRole.Role.Equals(iDRole2.Role)) {
role = (iDRole2.Role ?? iDRole.Role);
flag = true;
}
if (flag) {
if (list == null)
list = new List<IDRole>();
list.Add(new IDRole(authenticated, id, role));
}
}
}
}
if (list != null)
return new PrincipalPermission(list.ToArray());
return null;
}
public IPermission Union(IPermission other)
{
if (other == null)
return Copy();
if (!VerifyType(other))
throw new ArgumentException(System.SR.Format(System.SR.Argument_WrongType, GetType().FullName), "other");
PrincipalPermission principalPermission = (PrincipalPermission)other;
if (IsUnrestricted() || principalPermission.IsUnrestricted())
return new PrincipalPermission(PermissionState.Unrestricted);
IDRole[] array = new IDRole[_idArray.Length + principalPermission._idArray.Length];
Array.Copy(_idArray, array, _idArray.Length);
Array.Copy(principalPermission._idArray, 0, array, _idArray.Length, principalPermission._idArray.Length);
return new PrincipalPermission(array);
}
public override bool Equals(object obj)
{
IPermission permission = obj as IPermission;
if (obj != null && permission == null)
return false;
if (!IsSubsetOf(permission))
return false;
if (permission != null && !permission.IsSubsetOf(this))
return false;
return true;
}
public override int GetHashCode()
{
int num = 0;
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
num += iDRole.GetHashCode();
}
return num;
}
public IPermission Copy()
{
return new PrincipalPermission(_idArray);
}
public void Demand()
{
IPrincipal currentPrincipal = Thread.CurrentPrincipal;
if (currentPrincipal == null)
throw new SecurityException(System.SR.Security_PrincipalPermission);
if (_idArray == null)
return;
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
if (!iDRole.Authenticated || (currentPrincipal.Identity.IsAuthenticated && (iDRole.ID == null || string.Equals(currentPrincipal.Identity.Name, iDRole.ID, StringComparison.OrdinalIgnoreCase)) && (iDRole.Role == null || currentPrincipal.IsInRole(iDRole.Role))))
return;
}
throw new SecurityException(System.SR.Security_PrincipalPermission);
}
public SecurityElement ToXml()
{
SecurityElement securityElement = new SecurityElement("IPermission");
string str = "System.Security.Permissions.PrincipalPermission";
securityElement.AddAttribute("class", str + ", " + GetType().Module.Assembly.FullName.Replace('"', '\''));
securityElement.AddAttribute("version", "1");
if (_idArray != null) {
IDRole[] idArray = _idArray;
foreach (IDRole iDRole in idArray) {
securityElement.AddChild(iDRole.ToXml());
}
}
return securityElement;
}
public void FromXml(SecurityElement elem)
{
if (elem == null)
throw new ArgumentNullException("elem");
if (elem.Tag == null || (!elem.Tag.Equals("Permission") && !elem.Tag.Equals("IPermission")))
throw new ArgumentException(System.SR.Argument_NotAPermissionElement);
string text = elem.Attribute("version");
if (text == null || (text != null && !text.Equals("1")))
throw new ArgumentException(System.SR.Argument_InvalidXMLBadVersion);
if (elem.Children != null && elem.Children.Count != 0) {
int count = elem.Children.Count;
int num = 0;
_idArray = new IDRole[count];
foreach (object child in elem.Children) {
_idArray[num++] = new IDRole((SecurityElement)child);
}
} else
_idArray = Array.Empty<IDRole>();
}
public override string ToString()
{
return ToXml().ToString();
}
}
}