当前位置: 首页>>代码示例>>C#>>正文


C# PolicyStatement类代码示例

本文整理汇总了C#中System.Security.Policy.PolicyStatement的典型用法代码示例。如果您正苦于以下问题:C# PolicyStatement类的具体用法?C# PolicyStatement怎么用?C# PolicyStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


PolicyStatement类属于System.Security.Policy命名空间,在下文中一共展示了PolicyStatement类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Security;
using System.Security.Policy;
using System.Security.Principal;
using System.Security.Permissions;

class Members
{
    [STAThread]
    static void Main(string[] args)
    {
        // Create two new policy statements.
        PolicyStatement policyStatement = firstConstructorTest();
        PolicyStatement policyStatement2 = secondConstructorTest();

        // Add attributes to the first policy statement.
        policyStatement.Attributes = PolicyStatementAttribute.All;

        // Create a copy of the first policy statement.
        PolicyStatement policyStatementCopy = createCopy(policyStatement);
        addXmlMember(ref policyStatementCopy);

        summarizePolicyStatment(policyStatement);
        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Construct a PolicyStatement with an Unrestricted permission set.
    private static PolicyStatement firstConstructorTest() 
    {
        // Construct the permission set.
        PermissionSet permissions 
            = new PermissionSet(PermissionState.Unrestricted);
        permissions.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        permissions.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        // Create a policy statement based on the newly created permission
        // set.
        PolicyStatement policyStatement = new PolicyStatement(permissions);

        return policyStatement;
    }

    // Construct a PolicyStatement with an Unrestricted permission set and
    // the LevelFinal attribute.
    private static PolicyStatement secondConstructorTest()
    {
        // Construct the permission set.
        PermissionSet permissions =
            new PermissionSet(PermissionState.Unrestricted);
        permissions.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        permissions.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));

        PolicyStatementAttribute levelFinalAttribute = 
            PolicyStatementAttribute.LevelFinal;
        
        // Create a new policy statement with the specified permission set.
        // The LevelFinal attribute is set to prevent the evaluation of lower
        // policy levels in a resolve operation.
        PolicyStatement policyStatement =
            new PolicyStatement(permissions, levelFinalAttribute);

        return policyStatement;
    }

    // Add a named permission set to the specified PolicyStatement.
    private static void AddPermissions(ref PolicyStatement policyStatement)
    {
        // Construct a NamedPermissionSet with basic permissions.
        NamedPermissionSet allPerms = new NamedPermissionSet("allPerms");
        allPerms.AddPermission(
            new SecurityPermission(SecurityPermissionFlag.Execution));
        allPerms.AddPermission(
            new ZoneIdentityPermission(SecurityZone.MyComputer));
        allPerms.AddPermission(
            new SiteIdentityPermission("www.contoso.com"));

        policyStatement.PermissionSet = allPerms;
    }

    // If a class attribute is not found in the specified PolicyStatement,
    // add a child XML element with an added class attribute.
    private static void addXmlMember(ref PolicyStatement policyStatement) 
    {
        SecurityElement xmlElement = policyStatement.ToXml();
        if (xmlElement.Attribute("class") == null)
        {
            SecurityElement newElement = 
                new SecurityElement("PolicyStatement");
            newElement.AddAttribute("class", policyStatement.ToString());
            newElement.AddAttribute("version","1.1");

            newElement.AddChild(new SecurityElement("PermissionSet"));

            policyStatement.FromXml(newElement);

            Console.Write("Added the class attribute and modified its ");
            Console.WriteLine("version number.\n" + newElement.ToString());
        }
    }

    // Verify that the type of the specified object is a PolicyStatement type
    // then create a copy of the object.
    private static PolicyStatement createCopy(Object sourceObject)
    {
        PolicyStatement returnedStatement = new PolicyStatement(null);
        // Compare specified object type with the PolicyStatement type.
        if (sourceObject.GetType().Equals(typeof(PolicyStatement)))
        {
            returnedStatement = getCopy((PolicyStatement)sourceObject);
        }
        else
        {
            throw new ArgumentException("Expected the PolicyStatement type.");
        }

        return returnedStatement;
    }

    // Return a copy of the specified PolicyStatement if the result of the
    // Copy command is an equivalent object. Otherwise, return the
    // original PolicyStatement object.
    private static PolicyStatement getCopy(PolicyStatement policyStatement)
    {
        // Create an equivalent copy of the policy statement.
        PolicyStatement policyStatementCopy = policyStatement.Copy();

        // Compare the specified objects for equality.
        if (!policyStatementCopy.Equals(policyStatement))
        {
            return policyStatementCopy;
        } 
        else 
        {
            return policyStatement;
        }
    }

    // Summarize the attributes of the specified PolicyStatement on the
    // console window.
    private static void summarizePolicyStatment(
        PolicyStatement policyStatement)
    {
        // Retrieve the class path for policyStatement.
        string policyStatementClass = policyStatement.ToString();

        int hashCode = policyStatement.GetHashCode();

        string attributeString = "";
        // Retrieve the string representation of the PolicyStatement
        // attributes.
        if (policyStatement.AttributeString != null)
        {
            attributeString = policyStatement.AttributeString;
        }

        // Write a summary to the console window.
        Console.WriteLine("\n*** " + policyStatementClass + " summary ***");
        Console.Write("This PolicyStatement has been created with hash ");
        Console.Write("code(" + hashCode + ") ");

        Console.Write("and contains the following attributes: ");
        Console.WriteLine(attributeString);
    }
}
//
开发者ID:.NET开发者,项目名称:System.Security.Policy,代码行数:172,代码来源:PolicyStatement

输出:

Added the class attribute and modified the version number.



*** System.Security.Policy.PolicyStatement summary ***
PolicyStatement has been created with hash code(20) containing the
following attributes: Exclusive LevelFinal
This sample completed successfully; press Enter to exit.

示例2: new PolicyStatement

//引入命名空间
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Security;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        PolicyStatement policy = new PolicyStatement(new PermissionSet(PermissionState.Unrestricted), new PolicyStatementAttribute());
        UnionCodeGroup group = new UnionCodeGroup(new ZoneMembershipCondition(SecurityZone.Internet), policy);
        group.Name = "java2s";

        UnionCodeGroup childGroup = new UnionCodeGroup(new UrlMembershipCondition("http://www.java2s.com/"), policy);
        group.AddChild(childGroup);

        System.Console.WriteLine(group.ToXml().ToString());
    }
}
开发者ID:C#程序员,项目名称:System.Security.Policy,代码行数:27,代码来源:PolicyStatement


注:本文中的System.Security.Policy.PolicyStatement类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。