본문 바로가기
Application/C#.net

GetNamedPermissionSet

by 현이빈이 2009. 4. 10.
반응형
/// <summary>
/// Get a named permission set from the security policy
/// </summary>
/// <param name="name">Name of the permission set to retrieve</param>
/// <exception cref="ArgumentException">If name is null or empty</exception>
/// <returns>
/// The intersection of permission sets with the given name from all policy
/// levels, or an empty set if the name is not found
/// </returns>
public static PermissionSet GetNamedPermissionSet(string name)
{
    if(String.IsNullOrEmpty(name))
        throw new ArgumentException("name", "Cannot search for a permission set without a name");

    bool foundName = false;
    PermissionSet setIntersection = new PermissionSet(PermissionState.Unrestricted);

    // iterate over each policy level
    IEnumerator levelEnumerator = SecurityManager.PolicyHierarchy();
    while(levelEnumerator.MoveNext())
    {
        PolicyLevel level = levelEnumerator.Current as PolicyLevel;
        Debug.Assert(level != null);

        // if this level has defined a named permission set with the
        // given name, then intersect it with what we've retrieved
        // from all the previous levels
        PermissionSet levelSet = level.GetNamedPermissionSet(name);
        if(levelSet != null)
        {
            foundName = true;
            setIntersection = setIntersection.Intersect(levelSet);
        }
    }

     // Intersect() can return null for an empty set, so convert that
    // to an empty set object. Also return an empty set if we didn't find
    // the named permission set we were looking for
    if(setIntersection == null || !foundName)
        setIntersection = new PermissionSet(PermissionState.None);
    else
        setIntersection = new NamedPermissionSet(name, setIntersection);

    // if no named permission sets were found, return an empty set,
    // otherwise return the set that was found
    return setIntersection;
}


원문 : http://blogs.msdn.com/shawnfa/archive/2004/10/22/246549.aspx#247380
반응형