SocketPermission 建構函式

定義

初始化 SocketPermission 類別的新執行個體。

多載

SocketPermission(PermissionState)

初始化 SocketPermission 類別的新執行個體,允許不受限制存取 Socket 或不允許存取 Socket

SocketPermission(NetworkAccess, TransportType, String, Int32)

使用指定的使用權限,為指定的傳輸位址初始化 SocketPermission 類別的新執行個體。

SocketPermission(PermissionState)

來源:
SocketPermission.cs
來源:
SocketPermission.cs
來源:
SocketPermission.cs

初始化 SocketPermission 類別的新執行個體,允許不受限制存取 Socket 或不允許存取 Socket

public:
 SocketPermission(System::Security::Permissions::PermissionState state);
public SocketPermission (System.Security.Permissions.PermissionState state);
new System.Net.SocketPermission : System.Security.Permissions.PermissionState -> System.Net.SocketPermission
Public Sub New (state As PermissionState)

參數

state
PermissionState

其中一個 PermissionState 值。

範例

下列範例會 SocketPermission 使用列舉值建立 PermissionState

SocketPermission^ socketPermission1 = gcnew SocketPermission( PermissionState::Unrestricted );

// Create a 'SocketPermission' Object* for two ip addresses.
SocketPermission^ socketPermission2 = gcnew SocketPermission( PermissionState::None );
SecurityElement^ securityElement1 = socketPermission2->ToXml();
// 'SocketPermission' Object* for 'Connect' permission
SecurityElement^ securityElement2 = gcnew SecurityElement( "ConnectAccess" );
// Format to specify ip address are <ip-address>#<port>#<transport-type>
// First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and
// for 'All'ports for the ip-address.
SecurityElement^ securityElement3 = gcnew SecurityElement( "URI","192.168.144.238#-1#3" );
// Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and
// for 'All' ports for the ip-address.
SecurityElement^ securityElement4 = gcnew SecurityElement( "URI","192.168.144.240#-1#3" );
securityElement2->AddChild( securityElement3 );
securityElement2->AddChild( securityElement4 );
securityElement1->AddChild( securityElement2 );

// Obtain a 'SocketPermission' Object* using 'FromXml' method.
socketPermission2->FromXml( securityElement1 );

Console::WriteLine( "\nDisplays the result of FromXml method : \n" );
Console::WriteLine( socketPermission2 );

// Create another 'SocketPermission' Object* with two ip addresses.
// First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
SocketPermission^ socketPermission3 =
   gcnew SocketPermission( NetworkAccess::Connect,
      TransportType::All,
      "192.168.144.238",
      SocketPermission::AllPorts );

// Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
socketPermission3->AddPermission( NetworkAccess::Connect,
   TransportType::All,
   "192.168.144.239",
   SocketPermission::AllPorts );

Console::WriteLine( "Displays the result of AddPermission method : \n" );
Console::WriteLine( socketPermission3 );

// Find the intersection between two 'SocketPermission' objects.
socketPermission1 = dynamic_cast<SocketPermission^>(socketPermission2->Intersect( socketPermission3 ));

Console::WriteLine( "Displays the result of Intersect method :\n " );
Console::WriteLine( socketPermission1 );

// Demand that the calling method have the requsite socket permission.
socketPermission1->Demand();
  SocketPermission socketPermission1 = new SocketPermission(PermissionState.Unrestricted);

  // Create a 'SocketPermission' object for two ip addresses.
  SocketPermission socketPermission2 = new SocketPermission(PermissionState.None);
  SecurityElement securityElement1 = socketPermission2.ToXml();
  // 'SocketPermission' object for 'Connect' permission
  SecurityElement securityElement2 = new SecurityElement("ConnectAccess");
  // Format to specify ip address are <ip-address>#<port>#<transport-type>
  // First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and
// for 'All'ports for the ip-address.
  SecurityElement securityElement3 = new SecurityElement("URI", "192.168.144.238#-1#3");
  // Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and
// for 'All' ports for the ip-address.
  SecurityElement securityElement4 = new SecurityElement("URI", "192.168.144.240#-1#3");
  securityElement2.AddChild(securityElement3);
  securityElement2.AddChild(securityElement4);
  securityElement1.AddChild(securityElement2);
  
 // Obtain a 'SocketPermission' object using 'FromXml' method.
  socketPermission2.FromXml(securityElement1);

  Console.WriteLine("\nDisplays the result of FromXml method : \n");
  Console.WriteLine(socketPermission2.ToString());

  // Create another 'SocketPermission' object with two ip addresses.
  // First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
  SocketPermission socketPermission3 =
                  new SocketPermission(NetworkAccess.Connect,
                                       TransportType.All,
                                       "192.168.144.238",
                                       SocketPermission.AllPorts);

 // Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
  socketPermission3.AddPermission(NetworkAccess.Connect,
                                 TransportType.All,
                                 "192.168.144.239",
                                 SocketPermission.AllPorts);

  Console.WriteLine("Displays the result of AddPermission method : \n");
  Console.WriteLine(socketPermission3.ToString());

 // Find the intersection between two 'SocketPermission' objects.
  socketPermission1 = (SocketPermission)socketPermission2.Intersect(socketPermission3);

  Console.WriteLine("Displays the result of Intersect method :\n ");
  Console.WriteLine(socketPermission1.ToString());

  // Demand that the calling method have the requsite socket permission.
  socketPermission1.Demand();

Dim socketPermission1 As New SocketPermission(PermissionState.Unrestricted)

'Create a 'SocketPermission' object for two ip addresses.
Dim socketPermission2 As New SocketPermission(PermissionState.None)
Dim securityElement1 As SecurityElement = socketPermission2.ToXml()
''SocketPermission' object for 'Connect' permission
Dim securityElement2 As New SecurityElement("ConnectAccess")
'Format to specify ip address are <ip-address>#<port>#<transport-type>
'First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All'
' ports for the ip-address.
Dim securityElement3 As New SecurityElement("URI", "192.168.144.238#-1#3")
'Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and for 'All' ports for the ip-address.
Dim securityElement4 As New SecurityElement("URI", "192.168.144.240#-1#3")
securityElement2.AddChild(securityElement3)
securityElement2.AddChild(securityElement4)
securityElement1.AddChild(securityElement2)


'Obtain a 'SocketPermission' object using 'FromXml' method.
socketPermission2.FromXml(securityElement1)

Console.WriteLine(ControlChars.Cr + "Displays the result of FromXml method : " + ControlChars.Cr)
Console.WriteLine(socketPermission2.ToString())

'Create another 'SocketPermission' object with two ip addresses.
'First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
Dim socketPermission3 As New SocketPermission(NetworkAccess.Connect, TransportType.All, "192.168.144.238", SocketPermission.AllPorts)

'Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
socketPermission3.AddPermission(NetworkAccess.Connect, TransportType.All, "192.168.144.239", SocketPermission.AllPorts)

Console.WriteLine("Displays the result of AddPermission method : " + ControlChars.Cr)
Console.WriteLine(socketPermission3.ToString())

'Find the intersection between two 'SocketPermission' objects.
socketPermission1 = CType(socketPermission2.Intersect(socketPermission3), SocketPermission)

Console.WriteLine("Displays the result of Intersect method :" + ControlChars.Cr + " ")
Console.WriteLine(socketPermission1.ToString())
'Demand that the calling method have the requsite socket permission.
socketPermission1.Demand()

備註

SocketPermission如果實例是以 值建立 UnrestrictedPermissionStateSocketPermission 實例會傳遞所有需求。 的任何其他值 state 會導致 SocketPermission 實例失敗,除非使用 AddPermission 新增傳輸位址許可權,否則會失敗所有要求。

適用於

SocketPermission(NetworkAccess, TransportType, String, Int32)

來源:
SocketPermission.cs
來源:
SocketPermission.cs
來源:
SocketPermission.cs

使用指定的使用權限,為指定的傳輸位址初始化 SocketPermission 類別的新執行個體。

public:
 SocketPermission(System::Net::NetworkAccess access, System::Net::TransportType transport, System::String ^ hostName, int portNumber);
public SocketPermission (System.Net.NetworkAccess access, System.Net.TransportType transport, string hostName, int portNumber);
new System.Net.SocketPermission : System.Net.NetworkAccess * System.Net.TransportType * string * int -> System.Net.SocketPermission
Public Sub New (access As NetworkAccess, transport As TransportType, hostName As String, portNumber As Integer)

參數

access
NetworkAccess

其中一個 NetworkAccess 值。

transport
TransportType

其中一個 TransportType 值。

hostName
String

傳輸位址的主機名稱。

portNumber
Int32

傳輸位址的通訊埠編號。

例外狀況

hostNamenull

範例

下列範例會 SocketPermission 使用 NetworkAccess 列舉值、 TransportType 列舉值、主機名稱和埠號碼來建立 。

SocketPermission^ socketPermission1 = gcnew SocketPermission( PermissionState::Unrestricted );

// Create a 'SocketPermission' Object* for two ip addresses.
SocketPermission^ socketPermission2 = gcnew SocketPermission( PermissionState::None );
SecurityElement^ securityElement1 = socketPermission2->ToXml();
// 'SocketPermission' Object* for 'Connect' permission
SecurityElement^ securityElement2 = gcnew SecurityElement( "ConnectAccess" );
// Format to specify ip address are <ip-address>#<port>#<transport-type>
// First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and
// for 'All'ports for the ip-address.
SecurityElement^ securityElement3 = gcnew SecurityElement( "URI","192.168.144.238#-1#3" );
// Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and
// for 'All' ports for the ip-address.
SecurityElement^ securityElement4 = gcnew SecurityElement( "URI","192.168.144.240#-1#3" );
securityElement2->AddChild( securityElement3 );
securityElement2->AddChild( securityElement4 );
securityElement1->AddChild( securityElement2 );

// Obtain a 'SocketPermission' Object* using 'FromXml' method.
socketPermission2->FromXml( securityElement1 );

Console::WriteLine( "\nDisplays the result of FromXml method : \n" );
Console::WriteLine( socketPermission2 );

// Create another 'SocketPermission' Object* with two ip addresses.
// First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
SocketPermission^ socketPermission3 =
   gcnew SocketPermission( NetworkAccess::Connect,
      TransportType::All,
      "192.168.144.238",
      SocketPermission::AllPorts );

// Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
socketPermission3->AddPermission( NetworkAccess::Connect,
   TransportType::All,
   "192.168.144.239",
   SocketPermission::AllPorts );

Console::WriteLine( "Displays the result of AddPermission method : \n" );
Console::WriteLine( socketPermission3 );

// Find the intersection between two 'SocketPermission' objects.
socketPermission1 = dynamic_cast<SocketPermission^>(socketPermission2->Intersect( socketPermission3 ));

Console::WriteLine( "Displays the result of Intersect method :\n " );
Console::WriteLine( socketPermission1 );

// Demand that the calling method have the requsite socket permission.
socketPermission1->Demand();
  SocketPermission socketPermission1 = new SocketPermission(PermissionState.Unrestricted);

  // Create a 'SocketPermission' object for two ip addresses.
  SocketPermission socketPermission2 = new SocketPermission(PermissionState.None);
  SecurityElement securityElement1 = socketPermission2.ToXml();
  // 'SocketPermission' object for 'Connect' permission
  SecurityElement securityElement2 = new SecurityElement("ConnectAccess");
  // Format to specify ip address are <ip-address>#<port>#<transport-type>
  // First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and
// for 'All'ports for the ip-address.
  SecurityElement securityElement3 = new SecurityElement("URI", "192.168.144.238#-1#3");
  // Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and
// for 'All' ports for the ip-address.
  SecurityElement securityElement4 = new SecurityElement("URI", "192.168.144.240#-1#3");
  securityElement2.AddChild(securityElement3);
  securityElement2.AddChild(securityElement4);
  securityElement1.AddChild(securityElement2);
  
 // Obtain a 'SocketPermission' object using 'FromXml' method.
  socketPermission2.FromXml(securityElement1);

  Console.WriteLine("\nDisplays the result of FromXml method : \n");
  Console.WriteLine(socketPermission2.ToString());

  // Create another 'SocketPermission' object with two ip addresses.
  // First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
  SocketPermission socketPermission3 =
                  new SocketPermission(NetworkAccess.Connect,
                                       TransportType.All,
                                       "192.168.144.238",
                                       SocketPermission.AllPorts);

 // Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
  socketPermission3.AddPermission(NetworkAccess.Connect,
                                 TransportType.All,
                                 "192.168.144.239",
                                 SocketPermission.AllPorts);

  Console.WriteLine("Displays the result of AddPermission method : \n");
  Console.WriteLine(socketPermission3.ToString());

 // Find the intersection between two 'SocketPermission' objects.
  socketPermission1 = (SocketPermission)socketPermission2.Intersect(socketPermission3);

  Console.WriteLine("Displays the result of Intersect method :\n ");
  Console.WriteLine(socketPermission1.ToString());

  // Demand that the calling method have the requsite socket permission.
  socketPermission1.Demand();

Dim socketPermission1 As New SocketPermission(PermissionState.Unrestricted)

'Create a 'SocketPermission' object for two ip addresses.
Dim socketPermission2 As New SocketPermission(PermissionState.None)
Dim securityElement1 As SecurityElement = socketPermission2.ToXml()
''SocketPermission' object for 'Connect' permission
Dim securityElement2 As New SecurityElement("ConnectAccess")
'Format to specify ip address are <ip-address>#<port>#<transport-type>
'First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All'
' ports for the ip-address.
Dim securityElement3 As New SecurityElement("URI", "192.168.144.238#-1#3")
'Second 'SocketPermission' ip-address is '192.168.144.240' for 'All' transport types and for 'All' ports for the ip-address.
Dim securityElement4 As New SecurityElement("URI", "192.168.144.240#-1#3")
securityElement2.AddChild(securityElement3)
securityElement2.AddChild(securityElement4)
securityElement1.AddChild(securityElement2)


'Obtain a 'SocketPermission' object using 'FromXml' method.
socketPermission2.FromXml(securityElement1)

Console.WriteLine(ControlChars.Cr + "Displays the result of FromXml method : " + ControlChars.Cr)
Console.WriteLine(socketPermission2.ToString())

'Create another 'SocketPermission' object with two ip addresses.
'First 'SocketPermission' ip-address is '192.168.144.238' for 'All' transport types and for 'All' ports for the ip-address.
Dim socketPermission3 As New SocketPermission(NetworkAccess.Connect, TransportType.All, "192.168.144.238", SocketPermission.AllPorts)

'Second 'SocketPermission' ip-address is '192.168.144.239' for 'All' transport types and for 'All' ports for the ip-address.
socketPermission3.AddPermission(NetworkAccess.Connect, TransportType.All, "192.168.144.239", SocketPermission.AllPorts)

Console.WriteLine("Displays the result of AddPermission method : " + ControlChars.Cr)
Console.WriteLine(socketPermission3.ToString())

'Find the intersection between two 'SocketPermission' objects.
socketPermission1 = CType(socketPermission2.Intersect(socketPermission3), SocketPermission)

Console.WriteLine("Displays the result of Intersect method :" + ControlChars.Cr + " ")
Console.WriteLine(socketPermission1.ToString())
'Demand that the calling method have the requsite socket permission.
socketPermission1.Demand()

備註

這個建構函式會建立 , SocketPermission 控制指定 hostName 之 的存取,並使用 portNumber 指定的 transport

hostName可以是 DNS 名稱、IP 位址或指定的 IP 子網,例如 192.168.1.*。

portNumber可以是傳輸 SocketPermission.AllPorts 或 所定義的任何有效埠號碼。

適用於