/// <summary>Reconstructs a permission with a specified state from an XML encoding.</summary> /// <param name="elem">The XML encoding to use to reconstruct the permission. </param> /// <exception cref="T:System.ArgumentNullException">The <paramref name="elem" /> parameter is null. </exception> /// <exception cref="T:System.ArgumentException">The <paramref name="elem" /> parameter is not a valid permission element.-or- The <paramref name="elem" /> parameter's version number is not valid. </exception> public void FromXml(SecurityElement elem) { this.CheckSecurityElement(elem, "elem", 1, 1); this.principals.Clear(); if (elem.Children != null) { foreach (object obj in elem.Children) { SecurityElement securityElement = (SecurityElement)obj; if (securityElement.Tag != "Identity") { throw new ArgumentException("not IPermission/Identity"); } string name = securityElement.Attribute("ID"); string role = securityElement.Attribute("Role"); string text = securityElement.Attribute("Authenticated"); bool isAuthenticated = false; if (text != null) { try { isAuthenticated = bool.Parse(text); } catch { } } PrincipalPermission.PrincipalInfo value = new PrincipalPermission.PrincipalInfo(name, role, isAuthenticated); this.principals.Add(value); } } }
/// <summary>Creates an XML encoding of the permission and its current state.</summary> /// <returns>An XML encoding of the permission, including any state information.</returns> public SecurityElement ToXml() { SecurityElement securityElement = new SecurityElement("Permission"); Type type = base.GetType(); securityElement.AddAttribute("class", type.FullName + ", " + type.Assembly.ToString().Replace('"', '\'')); securityElement.AddAttribute("version", 1.ToString()); foreach (object obj in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj; SecurityElement securityElement2 = new SecurityElement("Identity"); if (principalInfo.Name != null) { securityElement2.AddAttribute("ID", principalInfo.Name); } if (principalInfo.Role != null) { securityElement2.AddAttribute("Role", principalInfo.Role); } if (principalInfo.IsAuthenticated) { securityElement2.AddAttribute("Authenticated", "true"); } securityElement.AddChild(securityElement2); } return(securityElement); }
/// <summary>Determines at run time whether the current principal matches the principal specified by the current permission.</summary> /// <exception cref="T:System.Security.SecurityException">The current principal does not pass the security check for the principal specified by the current permission.-or- The current <see cref="T:System.Security.Principal.IPrincipal" /> is null. </exception> public void Demand() { IPrincipal currentPrincipal = Thread.CurrentPrincipal; if (currentPrincipal == null) { throw new SecurityException("no Principal"); } if (this.principals.Count > 0) { bool flag = false; foreach (object obj in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj; if ((principalInfo.Name == null || principalInfo.Name == currentPrincipal.Identity.Name) && (principalInfo.Role == null || currentPrincipal.IsInRole(principalInfo.Role)) && ((principalInfo.IsAuthenticated && currentPrincipal.Identity.IsAuthenticated) || !principalInfo.IsAuthenticated)) { flag = true; break; } } if (!flag) { throw new SecurityException("Demand for principal refused."); } } }
/// <summary>Determines whether the current permission is a subset of the specified permission.</summary> /// <returns>true if the current permission is a subset of the specified permission; otherwise, false.</returns> /// <param name="target">A permission that is to be tested for the subset relationship. This permission must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is an object that is not of the same type as the current permission. </exception> public bool IsSubsetOf(IPermission target) { PrincipalPermission principalPermission = this.Cast(target); if (principalPermission == null) { return(this.IsEmpty()); } if (this.IsUnrestricted()) { return(principalPermission.IsUnrestricted()); } if (principalPermission.IsUnrestricted()) { return(true); } foreach (object obj in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj; bool flag = false; foreach (object obj2 in principalPermission.principals) { PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj2; if ((principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null) && (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null) && principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated) { flag = true; } } if (!flag) { return(false); } } return(true); }
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Permissions.PrincipalPermission" /> class with the specified <see cref="T:System.Security.Permissions.PermissionState" />.</summary> /// <param name="state">One of the <see cref="T:System.Security.Permissions.PermissionState" /> values. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="state" /> parameter is not a valid <see cref="T:System.Security.Permissions.PermissionState" />. </exception> public PrincipalPermission(PermissionState state) { this.principals = new ArrayList(); if (CodeAccessPermission.CheckPermissionState(state, true) == PermissionState.Unrestricted) { PrincipalPermission.PrincipalInfo value = new PrincipalPermission.PrincipalInfo(null, null, true); this.principals.Add(value); } }
/// <summary>Creates and returns a permission that is the intersection of the current permission and the specified permission.</summary> /// <returns>A new permission that represents the intersection of the current permission and the specified permission. This new permission will be null if the intersection is empty.</returns> /// <param name="target">A permission to intersect with the current permission. It must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not an instance of the same class as the current permission. </exception> public IPermission Intersect(IPermission target) { PrincipalPermission principalPermission = this.Cast(target); if (principalPermission == null) { return(null); } if (this.IsUnrestricted()) { return(principalPermission.Copy()); } if (principalPermission.IsUnrestricted()) { return(this.Copy()); } PrincipalPermission principalPermission2 = new PrincipalPermission(PermissionState.None); foreach (object obj in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj; foreach (object obj2 in principalPermission.principals) { PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj2; if (principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated) { string text = null; if (principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null) { text = principalInfo.Name; } else if (principalInfo.Name == null) { text = principalInfo2.Name; } string text2 = null; if (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null) { text2 = principalInfo.Role; } else if (principalInfo.Role == null) { text2 = principalInfo2.Role; } if (text != null || text2 != null) { PrincipalPermission.PrincipalInfo value = new PrincipalPermission.PrincipalInfo(text, text2, principalInfo.IsAuthenticated); principalPermission2.principals.Add(value); } } } } return((principalPermission2.principals.Count <= 0) ? null : principalPermission2); }
/// <summary>Returns a value indicating whether the current permission is unrestricted.</summary> /// <returns>true if the current permission is unrestricted; otherwise, false.</returns> public bool IsUnrestricted() { foreach (object obj in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj; if (principalInfo.Name == null && principalInfo.Role == null && principalInfo.IsAuthenticated) { return(true); } } return(false); }
/// <summary>Creates a permission that is the union of the current permission and the specified permission.</summary> /// <returns>A new permission that represents the union of the current permission and the specified permission.</returns> /// <param name="other">A permission to combine with the current permission. It must be of the same type as the current permission. </param> /// <exception cref="T:System.ArgumentException">The <paramref name="other" /> parameter is an object that is not of the same type as the current permission. </exception> public IPermission Union(IPermission other) { PrincipalPermission principalPermission = this.Cast(other); if (principalPermission == null) { return(this.Copy()); } if (this.IsUnrestricted() || principalPermission.IsUnrestricted()) { return(new PrincipalPermission(PermissionState.Unrestricted)); } PrincipalPermission principalPermission2 = new PrincipalPermission(this.principals); foreach (object obj in principalPermission.principals) { PrincipalPermission.PrincipalInfo value = (PrincipalPermission.PrincipalInfo)obj; principalPermission2.principals.Add(value); } return(principalPermission2); }
public override bool Equals(object obj) { if (obj == null) { return(false); } PrincipalPermission principalPermission = obj as PrincipalPermission; if (principalPermission == null) { return(false); } if (this.principals.Count != principalPermission.principals.Count) { return(false); } foreach (object obj2 in this.principals) { PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj2; bool flag = false; foreach (object obj3 in principalPermission.principals) { PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj3; if ((principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null) && (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null) && principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated) { flag = true; break; } } if (!flag) { return(false); } } return(true); }
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Permissions.PrincipalPermission" /> class for the specified <paramref name="name" />, <paramref name="role" />, and authentication status.</summary> /// <param name="name">The name of the <see cref="T:System.Security.Principal.IPrincipal" /> object's user. </param> /// <param name="role">The role of the <see cref="T:System.Security.Principal.IPrincipal" /> object's user (for example, Administrator). </param> /// <param name="isAuthenticated">true to signify that the user is authenticated; otherwise, false. </param> public PrincipalPermission(string name, string role, bool isAuthenticated) { this.principals = new ArrayList(); PrincipalPermission.PrincipalInfo value = new PrincipalPermission.PrincipalInfo(name, role, isAuthenticated); this.principals.Add(value); }