public void CheckSubset()
    {
// <Snippet1>
        // Create a WebPermission.
        WebPermission myWebPermission1 = new WebPermission();

        // Allow Connect access to the specified URLs.
        myWebPermission1.AddPermission(NetworkAccess.Connect, new Regex("http://www\\.contoso\\.com/.*",
                                                                        RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline));

        myWebPermission1.Demand();

// </Snippet1>

        // Create another WebPermission with the specified URL.
        WebPermission myWebPermission2 = new WebPermission(NetworkAccess.Connect, "http://www.contoso.com");

        // Check whether all callers higher in the call stack have been granted the permission.
        myWebPermission2.Demand();

// <Snippet2>

        WebPermission myWebPermission3 = null;

        // Check which permissions have the Connect access to more number of URLs.
        if (myWebPermission2.IsSubsetOf(myWebPermission1))
        {
            Console.WriteLine("\n WebPermission2 is the Subset of WebPermission1\n");
            myWebPermission3 = myWebPermission1;
        }
        else if (myWebPermission1.IsSubsetOf(myWebPermission2))
        {
            Console.WriteLine("\n WebPermission1 is the Subset of WebPermission2");
            myWebPermission3 = myWebPermission2;
        }
        else
        {
            // Create the third permission.
            myWebPermission3 = (WebPermission)myWebPermission1.Union(myWebPermission2);
        }

// </Snippet2>
        // Prints the attributes , values and childrens of XML encoded instances.

        Console.WriteLine("\nAttributes and Values of third WebPermission instance are : ");
        PrintKeysAndValues(myWebPermission3.ToXml().Attributes, myWebPermission3.ToXml().Children);
    }
Example #2
0
    public static void myIsSubsetExample()
    {
//<Snippet1>

        // Create the target permission.
        WebPermission targetPermission = new WebPermission();

        targetPermission.AddPermission(NetworkAccess.Connect, new Regex("www\\.contoso\\.com/Public/.*"));

        // Create the permission for a URI matching target.
        WebPermission connectPermission = new WebPermission();

        connectPermission.AddPermission(NetworkAccess.Connect, "www.contoso.com/Public/default.htm");

        //The following statement prints true.
        Console.WriteLine("Is the second URI a subset of the first one?: " + connectPermission.IsSubsetOf(targetPermission));

//</Snippet1>
    }