SiteIdentityPermission Class
Defines the identity permission for the Web site from which the code originates. This class cannot be inherited.
For a list of all members of this type, see SiteIdentityPermission Members.
System.Object
System.Security.CodeAccessPermission
System.Security.Permissions.SiteIdentityPermission
[Visual Basic] <Serializable> NotInheritable Public Class SiteIdentityPermission Inherits CodeAccessPermission [C#] [Serializable] public sealed class SiteIdentityPermission : CodeAccessPermission [C++] [Serializable] public __gc __sealed class SiteIdentityPermission : public CodeAccessPermission [JScript] public Serializable class SiteIdentityPermission extends CodeAccessPermission
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
Using this class, it is possible to ensure that callers are from a specific Web site. Site identity is only defined for code from URLs with the protocols of HTTP, HTTPS, and FTP. A site is the string between the "//" after the protocol of a URL and the following "/", if present, for example, www.fourthcoffee.com in the URL http://www.fourthcoffee.com/process/grind.htm. This excludes port numbers. If a given URL is http://www.fourthcoffee.com:8000/, the site is www.fourthcoffee.com, not www.fourthcoffee.com:8000.
Sites can be matched exactly, or by a wildcard ("*") prefix at the dot delimiter. For example, the site name string *.fourthcoffee.com matches fourthcoffee.com as well as www.fourthcoffee.com. Without a wildcard, the site name must be a precise match. The site name string * will match any site, but will not match code that has no site evidence.
CAUTION SiteIdentityPermission grants permission for all paths to the site, including both the URL and the IP address. To Deny access to a site, you must Deny all possible paths to the site. For example, if www.fourthcoffee.com is located at IP address 192.168.238.241, to Deny access to www.fourthcoffee.com, you must Deny www.fourthcoffee.com, 192.168.238.241 and any other path that you can use to access the site. A better technique to deal with multiple paths is to use a combination of PermitOnly and Deny. For more information on this subject and the use of PermitOnly with Deny, see Canonicalization Problems Using Deny in the Deny topic.
Example
[Visual Basic] ' This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods ' of the SiteIdentityPermission class. Imports System Imports System.Security Imports System.Security.Permissions Imports Microsoft.VisualBasic <Assembly: CLSCompliant(True)> Public Class SiteIdentityPermissionDemo ' IsSubsetOf determines whether the current permission is a subset of the specified permission. Private Function IsSubsetOfDemo() As Boolean Dim returnCodeCode As Boolean = True Dim site1, site2 As [String] Dim successFlag As Boolean Dim siteIdPerm1, siteIdPerm2 As SiteIdentityPermission Dim siteGen1 As New SiteGenerator() Dim siteGen2 As New SiteGenerator() siteGen1.ResetIndex() While siteGen1.CreateSite(siteIdPerm1, site1, successFlag) If siteIdPerm1 Is Nothing Or successFlag = False Then GoTo ContinueWhile1 End If siteGen2.ResetIndex() Console.WriteLine("**************************************************************************") While siteGen2.CreateSite(siteIdPerm2, site2, successFlag) Try If siteIdPerm2 Is Nothing Or successFlag = False Then GoTo ContinueWhile2 End If If siteIdPerm1.IsSubsetOf(siteIdPerm2) Then Console.WriteLine((site1 & " is a subset of " & site2)) Else Console.WriteLine((site1 & " is not a subset of " & site2)) End If Catch e As Exception Console.WriteLine(("An exception was thrown : " & e.ToString())) Return False End Try ContinueWhile2: End While ContinueWhile1: End While Return returnCodeCode End Function 'IsSubsetOfDemo ' Union creates a new permission that is the union of the current permission ' and the specified permission. Private Function UnionDemo() As Boolean Dim returnCodeCode As Boolean = True Dim site1, site2 As [String] Dim successFlag As Boolean Dim siteIdPerm1, siteIdPerm2, p3 As SiteIdentityPermission Dim siteGen1 As New SiteGenerator() Dim siteGen2 As New SiteGenerator() siteGen1.ResetIndex() While siteGen1.CreateSite(siteIdPerm1, site1, successFlag) If siteIdPerm1 Is Nothing Or successFlag = False Then GoTo ContinueWhile1 End If siteGen2.ResetIndex() Console.WriteLine("**************************************************************************") While siteGen2.CreateSite(siteIdPerm2, site2, successFlag) If siteIdPerm2 Is Nothing Or successFlag = False Then GoTo ContinueWhile2 End If Dim firstSite As [String] = IIf(site1 Is Nothing, "null", site1) Dim secondSite As [String] = IIf(site2 Is Nothing, "null", site2) Try p3 = CType(siteIdPerm1.Union(siteIdPerm2), SiteIdentityPermission) Dim thirdSite As [String] = IIf(p3.Site Is Nothing, "null", p3.Site.ToString()) If Not (p3 Is Nothing) Then Console.WriteLine(("The union of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = " & ControlChars.Lf & ControlChars.Tab & thirdSite & ControlChars.Lf)) Else Console.WriteLine(("The union of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = null." & ControlChars.Lf)) End If Catch ' Expected exception, result of the union is null. Console.WriteLine(("The union of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = null." & ControlChars.Lf)) End Try ContinueWhile2: End While ContinueWhile1: End While Return returnCodeCode End Function 'UnionDemo ' Intersect creates and returns a new permission that is the intersection of the ' current permission and the permission specified. Private Function IntersectDemo() As Boolean Dim returnCodeCode As Boolean = True Dim site1, site2 As [String] Dim successFlag As Boolean Dim siteIdPerm1, siteIdPerm2, p3 As SiteIdentityPermission Dim siteGen1 As New SiteGenerator() Dim siteGen2 As New SiteGenerator() siteGen1.ResetIndex() While siteGen1.CreateSite(siteIdPerm1, site1, successFlag) If siteIdPerm1 Is Nothing Or successFlag = False Then GoTo ContinueWhile1 End If siteGen2.ResetIndex() Console.WriteLine("**************************************************************************") While siteGen2.CreateSite(siteIdPerm2, site2, successFlag) If siteIdPerm2 Is Nothing Or successFlag = False Then GoTo ContinueWhile2 End If Dim firstSite As [String] = IIf(site1 Is Nothing, "null", site1) Dim secondSite As [String] = IIf(site2 Is Nothing, "null", site2) Try p3 = CType(siteIdPerm1.Intersect(siteIdPerm2), SiteIdentityPermission) Dim thirdSite As [String] = IIf(p3.Site Is Nothing, "null", p3.Site.ToString()) If Not (p3 Is Nothing) Then Console.WriteLine(("The intersection of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = " & thirdSite & ControlChars.Lf)) Else Console.WriteLine(("The intersection of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = null." & ControlChars.Lf)) End If Catch Console.WriteLine(("The intersection of " & firstSite & " and " & ControlChars.Lf & ControlChars.Tab & secondSite & " = null." & ControlChars.Lf)) End Try ContinueWhile2: End While ContinueWhile1: End While Return returnCodeCode End Function 'IntersectDemo 'Copy creates and returns an identical copy of the current permission. Private Function CopyDemo() As Boolean Dim returnCodeCode As Boolean = True Dim site1 As [String] Dim siteIdPerm1, siteIdPerm2 As SiteIdentityPermission Dim successFlag As Boolean Dim siteGen1 As New SiteGenerator() Dim siteGen2 As New SiteGenerator() siteGen1.ResetIndex() While siteGen1.CreateSite(siteIdPerm1, site1, successFlag) If siteIdPerm1 Is Nothing Or successFlag = False Then GoTo ContinueWhile1 End If siteGen2.ResetIndex() Console.WriteLine("**************************************************************************") Try siteIdPerm2 = CType(siteIdPerm1.Copy(), SiteIdentityPermission) If Not (siteIdPerm2 Is Nothing) Then Console.WriteLine(("The copy of " & siteIdPerm2.ToString() & " succeeded." & ControlChars.Lf)) End If Catch e As Exception Console.WriteLine(("The copy failed : " & siteIdPerm1.ToString() & e.ToString())) GoTo ContinueWhile1 End Try ContinueWhile1: End While Return returnCodeCode End Function 'CopyDemo ' ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a ' permission with the specified state from the XML encoding. Private Function ToFromXmlDemo() As Boolean Dim returnCodeCode As Boolean = True Dim site1 As [String] Dim siteIdPerm1, siteIdPerm2 As SiteIdentityPermission Dim successFlag As Boolean Dim siteGen1 As New SiteGenerator() Dim siteGen2 As New SiteGenerator() siteGen1.ResetIndex() While siteGen1.CreateSite(siteIdPerm1, site1, successFlag) If siteIdPerm1 Is Nothing Or successFlag = False Then GoTo ContinueWhile1 End If siteGen2.ResetIndex() Console.WriteLine("**************************************************************************") Try siteIdPerm2 = New SiteIdentityPermission(PermissionState.None.ToString()) siteIdPerm2.FromXml(siteIdPerm1.ToXml()) Dim result As Boolean = siteIdPerm2.Equals(siteIdPerm1) If siteIdPerm2.IsSubsetOf(siteIdPerm1) AndAlso siteIdPerm1.IsSubsetOf(siteIdPerm2) Then Console.WriteLine(("Result of ToFromXml = " & siteIdPerm2.ToString())) Else Console.WriteLine(siteIdPerm2.ToString()) Console.WriteLine(siteIdPerm1.ToString()) End If Catch e As Exception Console.WriteLine(("ToFromXml failed. " & e.ToString())) GoTo ContinueWhile1 End Try ContinueWhile1: End While Return returnCodeCode End Function 'ToFromXmlDemo ' Invoke all demos. Public Function RunDemo() As Boolean Dim returnCode As Boolean = True Dim tempReturnCode As Boolean ' Call the IsSubsetOf demo. tempReturnCode = IsSubsetOfDemo() If tempReturnCode Then Console.Out.WriteLine("The IsSubsetOf demo completed successfully.") Else Console.Out.WriteLine("subsetDemo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Union demo. tempReturnCode = UnionDemo() If tempReturnCode Then Console.Out.WriteLine("The Union demo completed successfully.") Else Console.Out.WriteLine("UnionDemo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Intersect demo. tempReturnCode = IntersectDemo() If tempReturnCode Then Console.Out.WriteLine("The Intersect demo completed successfully.") Else Console.Out.WriteLine("IntersectDemo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the Copy demo. tempReturnCode = CopyDemo() If tempReturnCode Then Console.Out.WriteLine("The Copy demo completed successfully.") Else Console.Out.WriteLine("CopyDemo failed.") End If returnCode = tempReturnCode AndAlso returnCode ' Call the ToFromXML demo. tempReturnCode = ToFromXmlDemo() If tempReturnCode Then Console.Out.WriteLine("The ToFromXML demo completed successfully.") Else Console.Out.WriteLine("ToFromXmlDemo failed.") End If returnCode = tempReturnCode AndAlso returnCode Return returnCode End Function 'RunDemo ' Test harness. Public Overloads Shared Sub Main(ByVal args() As [String]) Try Dim testcase As New SiteIdentityPermissionDemo() Dim returnCode As Boolean = testcase.RunDemo() If returnCode Then Console.Out.WriteLine("The SiteIdentityPermission demo completed successfully.") Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 100 Else Console.Out.WriteLine("The SiteIdentityPermission demo failed.") Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 101 End If Catch e As Exception Console.Out.WriteLine("The SiteIdentityPermission demo failed.") Console.WriteLine(e.ToString()) Console.Out.WriteLine("Press the Enter key to exit.") Dim consoleInput As String = Console.ReadLine() System.Environment.ExitCode = 101 End Try End Sub 'Main End Class 'SiteIdentityPermissionDemo ' This class generates SiteIdentityPermission objects. Friend Class SiteGenerator Private siteArray As String() = {"www.northwindtraders.northwindtraders.com", "*.northwindtraders.com", "*.margiestravel.com", "northwindtraders.com", "*northwindtraders.com", ""} ' Replace this array with Web sites of your own choosing. Private siteIndex As Integer = 0 Public Sub New() ResetIndex() End Sub 'New Public Sub ResetIndex() siteIndex = 0 End Sub 'ResetIndex ' CreateSite creates a SiteIdentityPermission. Public Function CreateSite(ByRef sitePerm As SiteIdentityPermission, ByRef site As String, ByRef successFlag As Boolean) As Boolean successFlag = True If siteIndex >= siteArray.Length Then sitePerm = New SiteIdentityPermission(PermissionState.None.ToString()) site = "null" Return False End If site = siteArray(siteIndex) siteIndex = siteIndex + 1 Try sitePerm = New SiteIdentityPermission(site.ToString()) Return True Catch e As ArgumentException Console.WriteLine(("An ArgumentException was thrown: " & e.Message.ToString())) successFlag = False If Not (site Is Nothing) AndAlso site <> "" Then Console.WriteLine((site & " is an invalid site.")) ElseIf site = "" Then Console.WriteLine("An empty string signifies an invalid site.") site = "an empty string" Else sitePerm = New SiteIdentityPermission(PermissionState.None.ToString()) Console.WriteLine(e.ToString()) site = "null" Return False End If sitePerm = New SiteIdentityPermission(PermissionState.None.ToString()) Return True End Try End Function 'CreateSite End Class 'SiteGenerator ' End of SiteGenerator. [C#] // This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods // of the SiteIdentityPermission class. using System; using System.Security; using System.Security.Permissions; [assembly:CLSCompliant(true)] public class SiteIdentityPermissionDemo { // IsSubsetOf determines whether the current permission is a subset of the specified permission. private bool IsSubsetOfDemo() { bool returnCodeCode = true; String site1,site2; bool successFlag; SiteIdentityPermission siteIdPerm1,siteIdPerm2; SiteGenerator siteGen1 = new SiteGenerator(); SiteGenerator siteGen2 = new SiteGenerator(); siteGen1.ResetIndex(); while(siteGen1.CreateSite(out siteIdPerm1, out site1, out successFlag)) { if(siteIdPerm1 == null | successFlag == false) continue; siteGen2.ResetIndex(); Console.WriteLine("**************************************************************************"); while(siteGen2.CreateSite(out siteIdPerm2, out site2, out successFlag)) { try { if(siteIdPerm2 == null | successFlag == false) continue; if(siteIdPerm1.IsSubsetOf(siteIdPerm2)) { Console.WriteLine(site1 + " is a subset of " + site2); } else { Console.WriteLine(site1 + " is not a subset of " + site2); } } catch (Exception e) { Console.WriteLine ( "An exception was thrown : " + e); return false; } } } return returnCodeCode; } // Union creates a new permission that is the union of the current permission // and the specified permission. private bool UnionDemo() { bool returnCodeCode = true; String site1,site2; bool successFlag; SiteIdentityPermission siteIdPerm1,siteIdPerm2,p3; SiteGenerator siteGen1 = new SiteGenerator(); SiteGenerator siteGen2 = new SiteGenerator(); siteGen1.ResetIndex(); while(siteGen1.CreateSite(out siteIdPerm1, out site1, out successFlag)) { if(siteIdPerm1 == null | successFlag == false) continue; siteGen2.ResetIndex(); Console.WriteLine("**************************************************************************"); while(siteGen2.CreateSite(out siteIdPerm2, out site2, out successFlag)) { if(siteIdPerm2 == null | successFlag == false) continue; String firstSite = site1 == null ? "null" : site1; String secondSite = site2 == null ? "null" : site2; try { p3 = (SiteIdentityPermission)siteIdPerm1.Union(siteIdPerm2); String thirdSite = p3.Site == null ? "null" : p3.Site; if(p3 != null) { Console.WriteLine("The union of " + firstSite + " and \n\t" + secondSite + " = \n\t" + thirdSite + "\n"); } else { Console.WriteLine("The union of " + firstSite + " and \n\t" + secondSite + " = null.\n"); } } catch { // Expected exception, result of the union is null. Console.WriteLine("The union of " + firstSite + " and \n\t" + secondSite + " = null.\n"); } } } return returnCodeCode; } // Intersect creates and returns a new permission that is the intersection of the // current permission and the permission specified. private bool IntersectDemo() { bool returnCodeCode = true; String site1,site2; bool successFlag; SiteIdentityPermission siteIdPerm1,siteIdPerm2,p3; SiteGenerator siteGen1 = new SiteGenerator(); SiteGenerator siteGen2 = new SiteGenerator(); siteGen1.ResetIndex(); while(siteGen1.CreateSite(out siteIdPerm1, out site1, out successFlag)) { if(siteIdPerm1 == null | successFlag == false) continue; siteGen2.ResetIndex(); Console.WriteLine("**************************************************************************"); while(siteGen2.CreateSite(out siteIdPerm2, out site2, out successFlag)) { if(siteIdPerm2 == null | successFlag == false) continue; String firstSite = site1 == null ? "null" : site1; String secondSite = site2 == null ? "null" : site2; try { p3 = (SiteIdentityPermission)siteIdPerm1.Intersect(siteIdPerm2); String thirdSite = p3.Site == null ? "null" : p3.Site; if(p3 != null) { Console.WriteLine("The intersection of " + firstSite + " and \n\t" + secondSite + " = " + thirdSite + "\n"); } else { Console.WriteLine("The intersection of " + firstSite + " and \n\t" + secondSite + " = null.\n"); } } catch { Console.WriteLine("The intersection of " + firstSite + " and \n\t" + secondSite + " = null.\n"); } } } return returnCodeCode; } //Copy creates and returns an identical copy of the current permission. private bool CopyDemo() { bool returnCodeCode = true; String site1; SiteIdentityPermission siteIdPerm1,siteIdPerm2; bool successFlag; SiteGenerator siteGen1 = new SiteGenerator(); SiteGenerator siteGen2 = new SiteGenerator(); siteGen1.ResetIndex(); while(siteGen1.CreateSite(out siteIdPerm1, out site1, out successFlag)) { if(siteIdPerm1 == null | successFlag == false) continue; siteGen2.ResetIndex(); Console.WriteLine("**************************************************************************"); try{ siteIdPerm2 = (SiteIdentityPermission)siteIdPerm1.Copy(); if (siteIdPerm2 != null ) { Console.WriteLine("The copy of " + siteIdPerm2.ToString() + " succeeded.\n"); } } catch(Exception e) { Console.WriteLine("The copy failed : " + siteIdPerm1.ToString() + e); continue; } } return returnCodeCode; } // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a // permission with the specified state from the XML encoding. private bool ToFromXmlDemo() { bool returnCodeCode = true; String site1; SiteIdentityPermission siteIdPerm1,siteIdPerm2; bool successFlag; SiteGenerator siteGen1 = new SiteGenerator(); SiteGenerator siteGen2 = new SiteGenerator(); siteGen1.ResetIndex(); while(siteGen1.CreateSite(out siteIdPerm1, out site1, out successFlag)) { if(siteIdPerm1 == null | successFlag == false) continue; siteGen2.ResetIndex(); Console.WriteLine("**************************************************************************"); try { siteIdPerm2= new SiteIdentityPermission(PermissionState.None); siteIdPerm2.FromXml(siteIdPerm1.ToXml()); bool result = siteIdPerm2.Equals(siteIdPerm1); if (siteIdPerm2.IsSubsetOf(siteIdPerm1) && siteIdPerm1.IsSubsetOf(siteIdPerm2)) { Console.WriteLine("Result of ToFromXml = " + siteIdPerm2.ToString()); } else { Console.WriteLine(siteIdPerm2.ToString()); Console.WriteLine(siteIdPerm1.ToString()); } } catch(Exception e) { Console.WriteLine("ToFromXml failed. " + e); continue; } } return returnCodeCode; } // Invoke all demos. public bool RunDemo() { bool returnCode=true; bool tempReturnCode; // Call the IsSubsetOf demo. if(tempReturnCode= IsSubsetOfDemo())Console.Out.WriteLine("The IsSubsetOf demo completed successfully."); else Console.Out.WriteLine("subsetDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Union demo. if(tempReturnCode= UnionDemo())Console.Out.WriteLine("The Union demo completed successfully."); else Console.Out.WriteLine("UnionDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Intersect demo. if(tempReturnCode= IntersectDemo())Console.Out.WriteLine("The Intersect demo completed successfully."); else Console.Out.WriteLine("IntersectDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Copy demo. if(tempReturnCode= CopyDemo())Console.Out.WriteLine("The Copy demo completed successfully."); else Console.Out.WriteLine("CopyDemo failed."); returnCode=tempReturnCode && returnCode; // Call the ToFromXML demo. if(tempReturnCode= ToFromXmlDemo())Console.Out.WriteLine("The ToFromXML demo completed successfully."); else Console.Out.WriteLine("ToFromXmlDemo failed."); returnCode=tempReturnCode && returnCode; return ( returnCode ); } // Test harness. public static void Main(String[] args) { try { SiteIdentityPermissionDemo testcase = new SiteIdentityPermissionDemo(); bool returnCode = testcase.RunDemo(); if (returnCode) { Console.Out.WriteLine("The SiteIdentityPermission demo completed successfully."); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 100; } else { Console.Out.WriteLine("The SiteIdentityPermission demo failed."); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 101; } } catch(Exception e) { Console.Out.WriteLine("The SiteIdentityPermission demo failed."); Console.WriteLine(e.ToString()); Console.Out.WriteLine("Press the Enter key to exit."); string consoleInput = Console.ReadLine(); System.Environment.ExitCode = 101; } } } // This class generates SiteIdentityPermission objects. internal class SiteGenerator { private string[] siteArray = // Replace this array with Web sites of your own choosing. {"www.northwindtraders.northwindtraders.com", "*.northwindtraders.com", "*.margiestravel.com","northwindtraders.com", "*northwindtraders.com",""}; private int siteIndex = 0; public SiteGenerator() { ResetIndex(); } public void ResetIndex() { siteIndex = 0; } // CreateSite creates a SiteIdentityPermission. public bool CreateSite(out SiteIdentityPermission sitePerm, out string site, out bool successFlag) { successFlag = true; if(siteIndex >= siteArray.Length) { sitePerm = new SiteIdentityPermission(PermissionState.None); site="null"; return false; } site = siteArray[siteIndex++]; try { sitePerm = new SiteIdentityPermission(site); return true; } catch(ArgumentException e) { Console.WriteLine("An ArgumentException was thrown: " + e.Message); successFlag = false; if (site != null && site != "") { Console.WriteLine(site + " is an invalid site."); } else if (site == "") { Console.WriteLine("An empty string signifies an invalid site."); site = "an empty string"; } else { sitePerm = new SiteIdentityPermission(PermissionState.None); Console.WriteLine(e); site = "null"; return false; } sitePerm = new SiteIdentityPermission(PermissionState.None); return true; } } } // End of SiteGenerator. [C++] // This sample demonstrates the IsSubsetOf, Union, Intersect, Copy, ToXml and FromXml methods // of the SiteIdentityPermission class. #using <mscorlib.dll> using namespace System; using namespace System::Security; using namespace System::Security::Permissions; using namespace System::Runtime::InteropServices; [assembly:CLSCompliant(true)]; // This class generates SiteIdentityPermission objects. private __gc class SiteGenerator { private: String* siteArray[]; int siteIndex; public: SiteGenerator() { // Replace this array with Web sites of your own choosing. String* temp[] = {S"www.northwindtraders.northwindtraders.com", S"*.northwindtraders.com", S"*.margiestravel.com", S"northwindtraders.com", S"*northwindtraders.com", S""}; siteArray = temp; ResetIndex(); } void ResetIndex() { siteIndex = 0; } // CreateSite creates a SiteIdentityPermission. bool CreateSite( [Out] SiteIdentityPermission** sitePerm, [Out] String** site, [Out] bool* successFlag) { *successFlag = true; if (siteIndex >= siteArray->Length) { *sitePerm = new SiteIdentityPermission(PermissionState::None); *site=S"null"; return false; } *site = siteArray[siteIndex++]; try { *sitePerm = new SiteIdentityPermission(*site); return true; } catch (ArgumentException* e) { Console::WriteLine(S"An ArgumentException was thrown: {0}", e->Message); *successFlag = false; if (*site != 0 && !(*site)->Equals(S"")) { Console::WriteLine(S"{0} is an invalid site.", *site); } else if ((*site)->Equals(S"")) { Console::WriteLine(S"An empty string signifies an invalid site."); *site = S"an empty string"; } else { *sitePerm = new SiteIdentityPermission(PermissionState::None); Console::WriteLine(e); *site = S"null"; return false; } *sitePerm = new SiteIdentityPermission(PermissionState::None); return true; } } }; // End of SiteGenerator. public __gc class SiteIdentityPermissionDemo { // IsSubsetOf determines whether the current permission is a subset of the specified permission. bool IsSubsetOfDemo() { bool returnCodeCode = true; String* site1, *site2; bool successFlag; SiteIdentityPermission* siteIdPerm1, *siteIdPerm2; SiteGenerator* siteGen1 = new SiteGenerator(); SiteGenerator* siteGen2 = new SiteGenerator(); siteGen1->ResetIndex(); while(siteGen1->CreateSite(&siteIdPerm1, &site1, &successFlag)) { if (siteIdPerm1 == 0 || successFlag == false) continue; siteGen2->ResetIndex(); Console::WriteLine(S"**************************************************************************"); while(siteGen2->CreateSite(&siteIdPerm2, &site2, &successFlag)) { try { if (siteIdPerm2 == 0 || successFlag == false) continue; if (siteIdPerm1->IsSubsetOf(siteIdPerm2)) { Console::WriteLine(S"{0} is a subset of {1}", site1, site2); } else { Console::WriteLine(S"{0} is not a subset of {1}", site1, site2); } } catch (Exception* e) { Console::WriteLine (S"An exception was thrown : {0}", e); return false; } } } return returnCodeCode; } // Union creates a new permission that is the union of the current permission // and the specified permission. bool UnionDemo() { bool returnCodeCode = true; String* site1, *site2; bool successFlag; SiteIdentityPermission* siteIdPerm1, *siteIdPerm2, *p3; SiteGenerator* siteGen1 = new SiteGenerator(); SiteGenerator* siteGen2 = new SiteGenerator(); siteGen1->ResetIndex(); while(siteGen1->CreateSite(&siteIdPerm1, &site1, &successFlag)) { if (siteIdPerm1 == 0 || successFlag == false) continue; siteGen2->ResetIndex(); Console::WriteLine(S"**************************************************************************"); while(siteGen2->CreateSite(&siteIdPerm2, &site2, &successFlag)) { if (siteIdPerm2 == 0 || successFlag == false) continue; String* firstSite = site1 == 0 ? S"null" : site1; String* secondSite = site2 == 0 ? S"null" : site2; try { p3 = dynamic_cast<SiteIdentityPermission*>(siteIdPerm1->Union(siteIdPerm2)); String* thirdSite = p3->Site == 0 ? S"null" : p3->Site; if (p3 != 0) { Console::WriteLine(S"The union of {0} and \n\t{1} = \n\t{2}\n", firstSite, secondSite, thirdSite); } else { Console::WriteLine(S"The union of {0} and \n\t{1} = null.\n", firstSite, secondSite); } } catch (Exception*) { // Expected exception, result of the union is 0. Console::WriteLine(S"The union of {0} and \n\t{1} = null.\n", firstSite, secondSite); } } } return returnCodeCode; } // Intersect creates and returns a new permission that is the intersection of the // current permission and the permission specified. bool IntersectDemo() { bool returnCodeCode = true; String* site1, *site2; bool successFlag; SiteIdentityPermission *siteIdPerm1, *siteIdPerm2, *p3; SiteGenerator* siteGen1 = new SiteGenerator(); SiteGenerator* siteGen2 = new SiteGenerator(); siteGen1->ResetIndex(); while(siteGen1->CreateSite(&siteIdPerm1, &site1, &successFlag)) { if (siteIdPerm1 == 0 || successFlag == false) continue; siteGen2->ResetIndex(); Console::WriteLine(S"**************************************************************************"); while(siteGen2->CreateSite(&siteIdPerm2, &site2, &successFlag)) { if (siteIdPerm2 == 0 || successFlag == false) continue; String* firstSite = site1 == 0 ? S"0" : site1; String* secondSite = site2 == 0 ? S"0" : site2; try { p3 = dynamic_cast<SiteIdentityPermission*>(siteIdPerm1->Intersect(siteIdPerm2)); String* thirdSite = p3->Site == 0 ? S"0" : p3->Site; if (p3 != 0) { Console::WriteLine(S"The intersection of {0} and \n\t{1} = {2}\n", firstSite, secondSite, thirdSite); } else { Console::WriteLine(S"The intersection of {0} and \n\t{1} = null.\n", firstSite, secondSite); } } catch (Exception*) { Console::WriteLine(S"The intersection of {0} and \n\t{1} = null.\n", firstSite, secondSite); } } } return returnCodeCode; } //Copy creates and returns an identical copy of the current permission. bool CopyDemo() { bool returnCodeCode = true; String* site1; SiteIdentityPermission* siteIdPerm1, *siteIdPerm2; bool successFlag; SiteGenerator* siteGen1 = new SiteGenerator(); SiteGenerator* siteGen2 = new SiteGenerator(); siteGen1->ResetIndex(); while(siteGen1->CreateSite(&siteIdPerm1, &site1, &successFlag)) { if (siteIdPerm1 == 0 || successFlag == false) continue; siteGen2->ResetIndex(); Console::WriteLine(S"**************************************************************************"); try { siteIdPerm2 = dynamic_cast<SiteIdentityPermission*>(siteIdPerm1->Copy()); if (siteIdPerm2 != 0) { Console::WriteLine(S"The copy of {0} succeeded.\n", siteIdPerm2); } } catch (Exception* e) { Console::WriteLine(S"The copy failed : {0}{1}", siteIdPerm1, e); continue; } } return returnCodeCode; } // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a // permission with the specified state from the XML encoding. bool ToFromXmlDemo() { bool returnCodeCode = true; String* site1; SiteIdentityPermission* siteIdPerm1, *siteIdPerm2; bool successFlag; SiteGenerator* siteGen1 = new SiteGenerator(); SiteGenerator* siteGen2 = new SiteGenerator(); siteGen1->ResetIndex(); while(siteGen1->CreateSite(&siteIdPerm1, &site1, &successFlag)) { if (siteIdPerm1 == 0 || successFlag == false) continue; siteGen2->ResetIndex(); Console::WriteLine(S"**************************************************************************"); try { siteIdPerm2 = new SiteIdentityPermission(PermissionState::None); siteIdPerm2->FromXml(siteIdPerm1->ToXml()); bool result = siteIdPerm2->Equals(siteIdPerm1); if (siteIdPerm2->IsSubsetOf(siteIdPerm1) && siteIdPerm1->IsSubsetOf(siteIdPerm2)) { Console::WriteLine(S"Result of ToFromXml = {0}", siteIdPerm2); } else { Console::WriteLine(siteIdPerm2); Console::WriteLine(siteIdPerm1); } } catch (Exception* e) { Console::WriteLine(S"ToFromXml failed. {0}", e); continue; } } return returnCodeCode; } // Invoke all demos. public: bool RunDemo() { bool returnCode=true; bool tempReturnCode; // Call the IsSubsetOf demo. if (tempReturnCode= IsSubsetOfDemo())Console::WriteLine(S"The IsSubsetOf demo completed successfully."); else Console::WriteLine(S"subsetDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Union demo. if (tempReturnCode= UnionDemo())Console::WriteLine(S"The Union demo completed successfully."); else Console::WriteLine(S"UnionDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Intersect demo. if (tempReturnCode= IntersectDemo())Console::WriteLine(S"The Intersect demo completed successfully."); else Console::WriteLine(S"IntersectDemo failed."); returnCode=tempReturnCode && returnCode; // Call the Copy demo. if (tempReturnCode= CopyDemo())Console::WriteLine(S"The Copy demo completed successfully."); else Console::WriteLine(S"CopyDemo failed."); returnCode=tempReturnCode && returnCode; // Call the ToFromXML demo. if (tempReturnCode= ToFromXmlDemo())Console::WriteLine(S"The ToFromXML demo completed successfully."); else Console::WriteLine(S"ToFromXmlDemo failed."); returnCode=tempReturnCode && returnCode; return (returnCode); } }; // Test harness. int main() { try { SiteIdentityPermissionDemo* testcase = new SiteIdentityPermissionDemo(); bool returnCode = testcase->RunDemo(); if (returnCode) { Console::WriteLine(S"The SiteIdentityPermission demo completed successfully."); Console::WriteLine(S"Press the Enter key to exit."); Console::ReadLine(); System::Environment::ExitCode = 100; } else { Console::WriteLine(S"The SiteIdentityPermission demo failed."); Console::WriteLine(S"Press the Enter key to exit."); Console::ReadLine(); System::Environment::ExitCode = 101; } } catch (Exception* e) { Console::WriteLine(S"The SiteIdentityPermission demo failed."); Console::WriteLine(e); Console::WriteLine(S"Press the Enter key to exit."); Console::ReadLine(); System::Environment::ExitCode = 101; } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Security.Permissions
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Mscorlib (in Mscorlib.dll)
See Also
SiteIdentityPermission Members | System.Security.Permissions Namespace | Permissions | Requesting Permissions | SiteIdentityPermissionAttribute | Site | SiteMembershipCondition