SocketPermission SocketPermission SocketPermission SocketPermission Class

정의

전송 주소에 대해 연결을 만들거나 허용하는 권한을 제어합니다.Controls rights to make or accept connections on a transport address.

public ref class SocketPermission sealed : System::Security::CodeAccessPermission, System::Security::Permissions::IUnrestrictedPermission
[System.Serializable]
public sealed class SocketPermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
type SocketPermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
Public NotInheritable Class SocketPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
상속
특성
구현

예제

다음 예제에서는 사용 하는 방법에 설명 합니다 SocketPermission 설정, 변경 및 다양 한 소켓 액세스 제한을 적용 하는 클래스입니다.The following example demonstrates how to use the SocketPermission class to set, change, and enforce various socket access restrictions.

// Creates a SocketPermission restricting access to and from all URIs.
SocketPermission^ mySocketPermission1 = gcnew SocketPermission( PermissionState::None );

// The socket to which this permission will apply will allow connections from www.contoso.com.
mySocketPermission1->AddPermission( NetworkAccess::Accept, TransportType::Tcp,  "www.contoso.com", 11000 );

// Creates a SocketPermission which will allow the target Socket to connect with www.southridgevideo.com.
SocketPermission^ mySocketPermission2 = gcnew SocketPermission( NetworkAccess::Connect,TransportType::Tcp, "www.southridgevideo.com",11002 );

// Creates a SocketPermission from the union of two SocketPermissions.
SocketPermission^ mySocketPermissionUnion =
   (SocketPermission^)( mySocketPermission1->Union( mySocketPermission2 ) );

// Checks to see if the union was successfully created by using the IsSubsetOf method.
if ( mySocketPermission1->IsSubsetOf( mySocketPermissionUnion ) &&
   mySocketPermission2->IsSubsetOf( mySocketPermissionUnion ) )
{
   Console::WriteLine(  "This union contains permissions from both mySocketPermission1 and mySocketPermission2" );
   
   // Prints the allowable accept URIs to the console.
   Console::WriteLine(  "This union accepts connections on :" );

   IEnumerator^ myEnumerator = mySocketPermissionUnion->AcceptList;
   while ( myEnumerator->MoveNext() )
   {
      Console::WriteLine( safe_cast<EndpointPermission^>( myEnumerator->Current )->ToString() );
   }
   
   // Prints the allowable connect URIs to the console.
   Console::WriteLine(  "This union permits connections to :" );

   myEnumerator = mySocketPermissionUnion->ConnectList;
   while ( myEnumerator->MoveNext() )
   {
      Console::WriteLine( safe_cast<EndpointPermission^>( myEnumerator->Current )->ToString() );
   }
}

// Creates a SocketPermission from the intersect of two SocketPermissions.
SocketPermission^ mySocketPermissionIntersect =
   (SocketPermission^)( mySocketPermission1->Intersect( mySocketPermissionUnion ) );

// mySocketPermissionIntersect should now contain the permissions of mySocketPermission1.
if ( mySocketPermission1->IsSubsetOf( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "This is expected" );
}

// mySocketPermissionIntersect should not contain the permissios of mySocketPermission2.
if ( mySocketPermission2->IsSubsetOf( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "This should not print" );
}

// Creates a copy of the intersect SocketPermission.
SocketPermission^ mySocketPermissionIntersectCopy =
   (SocketPermission^)( mySocketPermissionIntersect->Copy() );
if ( mySocketPermissionIntersectCopy->Equals( mySocketPermissionIntersect ) )
{
   Console::WriteLine(  "Copy successfull" );
}

// Converts a SocketPermission to XML format and then immediately converts it back to a SocketPermission.
mySocketPermission1->FromXml( mySocketPermission1->ToXml() );

// Checks to see if permission for this socket resource is unrestricted.  If it is, then there is no need to
// demand that permissions be enforced.
if ( mySocketPermissionUnion->IsUnrestricted() )
{
   //Do nothing.  There are no restrictions.
}
else
{
   // Enforces the permissions found in mySocketPermissionUnion on any Socket Resources used below this statement. 
   mySocketPermissionUnion->Demand();
}

IPHostEntry^ myIpHostEntry = Dns::Resolve(  "www.contoso.com" );
IPEndPoint^ myLocalEndPoint = gcnew IPEndPoint( myIpHostEntry->AddressList[ 0 ], 11000 );

Socket^ s = gcnew Socket( myLocalEndPoint->Address->AddressFamily,
   SocketType::Stream,
   ProtocolType::Tcp );
try
{
   s->Connect( myLocalEndPoint );
}
catch ( Exception^ e ) 
{
   Console::Write(  "Exception Thrown: " );
   Console::WriteLine( e->ToString() );
}

// Perform all socket operations in here.
s->Close();

     // Creates a SocketPermission restricting access to and from all URIs.
     SocketPermission mySocketPermission1 = new SocketPermission(PermissionState.None);

     // The socket to which this permission will apply will allow connections from www.contoso.com.
     mySocketPermission1.AddPermission(NetworkAccess.Accept, TransportType.Tcp, "www.contoso.com", 11000);

     // Creates a SocketPermission which will allow the target Socket to connect with www.southridgevideo.com.
     SocketPermission mySocketPermission2 =
                                new SocketPermission(NetworkAccess.Connect, TransportType.Tcp, "www.southridgevideo.com", 11002);

     // Creates a SocketPermission from the union of two SocketPermissions.
     SocketPermission mySocketPermissionUnion = 
                                (SocketPermission)mySocketPermission1.Union(mySocketPermission2);

     // Checks to see if the union was successfully created by using the IsSubsetOf method.
     if (mySocketPermission1.IsSubsetOf(mySocketPermissionUnion) && 
           mySocketPermission2.IsSubsetOf(mySocketPermissionUnion)){
          Console.WriteLine("This union contains permissions from both mySocketPermission1 and mySocketPermission2"); 

          // Prints the allowable accept URIs to the console.
          Console.WriteLine("This union accepts connections on :");

          IEnumerator myEnumerator = mySocketPermissionUnion.AcceptList;
       while (myEnumerator.MoveNext()) {
               Console.WriteLine(((EndpointPermission)myEnumerator.Current).ToString());
            }      

             // Prints the allowable connect URIs to the console.
          Console.WriteLine("This union permits connections to :");

          myEnumerator = mySocketPermissionUnion.ConnectList;
       while (myEnumerator.MoveNext()) {
               Console.WriteLine(((EndpointPermission)myEnumerator.Current).ToString());
            }      

           }


     // Creates a SocketPermission from the intersect of two SocketPermissions.
     SocketPermission mySocketPermissionIntersect = 
                               (SocketPermission)mySocketPermission1.Intersect(mySocketPermissionUnion);

     // mySocketPermissionIntersect should now contain the permissions of mySocketPermission1.
     if (mySocketPermission1.IsSubsetOf(mySocketPermissionIntersect)){
          Console.WriteLine("This is expected");
     }
    // mySocketPermissionIntersect should not contain the permissios of mySocketPermission2.
     if (mySocketPermission2.IsSubsetOf(mySocketPermissionIntersect)){
          Console.WriteLine("This should not print");
     }



// Creates a copy of the intersect SocketPermission.
     SocketPermission mySocketPermissionIntersectCopy = 
                               (SocketPermission)mySocketPermissionIntersect.Copy();

     if (mySocketPermissionIntersectCopy.Equals(mySocketPermissionIntersect)){
     Console.WriteLine("Copy successfull");
     }


     // Converts a SocketPermission to XML format and then immediately converts it back to a SocketPermission.
     mySocketPermission1.FromXml(mySocketPermission1.ToXml());
     

     // Checks to see if permission for this socket resource is unrestricted.  If it is, then there is no need to
     // demand that permissions be enforced.
     if (mySocketPermissionUnion.IsUnrestricted()){
        
          //Do nothing.  There are no restrictions.

     }
     else{
         // Enforces the permissions found in mySocketPermissionUnion on any Socket Resources used below this statement. 
         mySocketPermissionUnion.Demand();
     }

    IPHostEntry myIpHostEntry = Dns.Resolve("www.contoso.com");
    IPEndPoint myLocalEndPoint = new IPEndPoint(myIpHostEntry.AddressList[0], 11000);

       Socket s = new Socket(myLocalEndPoint.Address.AddressFamily,
                                   SocketType.Stream,
                                         ProtocolType.Tcp);
       try{
            s.Connect(myLocalEndPoint);
       }
       catch (Exception e){
            Console.WriteLine("Exception Thrown: " + e.ToString());
       } 

      // Perform all socket operations in here.
      
      s.Close();
   ' Creates a SocketPermission restricting access to and from all URIs.
   Dim mySocketPermission1 As New SocketPermission(PermissionState.None)
   
   ' The socket to which this permission will apply will allow connections from www.contoso.com.
   mySocketPermission1.AddPermission(NetworkAccess.Accept, TransportType.Tcp, "www.contoso.com", 11000)
   
   ' Creates a SocketPermission which will allow the target Socket to connect with www.southridgevideo.com.
   Dim mySocketPermission2 As New SocketPermission(NetworkAccess.Connect, TransportType.Tcp, "www.southridgevideo.com", 11002)
   
   ' Creates a SocketPermission from the union of two SocketPermissions.
   Dim mySocketPermissionUnion As SocketPermission = CType(mySocketPermission1.Union(mySocketPermission2), SocketPermission)
   
   ' Checks to see if the union was successfully created by using the IsSubsetOf method.
   If mySocketPermission1.IsSubsetOf(mySocketPermissionUnion) And mySocketPermission2.IsSubsetOf(mySocketPermissionUnion) Then
      Console.WriteLine("This union contains permissions from both mySocketPermission1 and mySocketPermission2")
      
      ' Prints the allowable accept URIs to the console.
      Console.WriteLine("This union accepts connections on :")
      
      Dim myEnumerator As IEnumerator = mySocketPermissionUnion.AcceptList
      While myEnumerator.MoveNext()
         Console.WriteLine(CType(myEnumerator.Current, EndpointPermission).ToString())
      End While
      
      Console.WriteLine("This union establishes connections on : ")
      
      ' Prints the allowable connect URIs to the console.
      Console.WriteLine("This union permits connections to :")
      
      myEnumerator = mySocketPermissionUnion.ConnectList
      While myEnumerator.MoveNext()
         Console.WriteLine(CType(myEnumerator.Current, EndpointPermission).ToString())
      End While
   End If 
   ' Creates a SocketPermission from the intersect of two SocketPermissions.
   Dim mySocketPermissionIntersect As SocketPermission = CType(mySocketPermission1.Intersect(mySocketPermissionUnion), SocketPermission)
   
   ' mySocketPermissionIntersect should now contain the permissions of mySocketPermission1.
   If mySocketPermission1.IsSubsetOf(mySocketPermissionIntersect) Then
      Console.WriteLine("This is expected")
   End If
   ' mySocketPermissionIntersect should not contain the permissios of mySocketPermission2.
   If mySocketPermission2.IsSubsetOf(mySocketPermissionIntersect) Then
      Console.WriteLine("This should not print")
   End If
   
   ' Creates a copy of the intersect SocketPermission.
   Dim mySocketPermissionIntersectCopy As SocketPermission = CType(mySocketPermissionIntersect.Copy(), SocketPermission)
   
   If mySocketPermissionIntersectCopy.Equals(mySocketPermissionIntersect) Then
      Console.WriteLine("Copy successfull")
   End If
   ' Converts a SocketPermission to XML format and then immediately converts it back to a SocketPermission.
   mySocketPermission1.FromXml(mySocketPermission1.ToXml())
   
   
   ' Checks to see if permission for this socket resource is unrestricted.  If it is, then there is no need to
   ' demand that permissions be enforced.
   If mySocketPermissionUnion.IsUnrestricted() Then
   
   'Do nothing.  There are no restrictions.
   Else
      ' Enforces the permissions found in mySocketPermissionUnion on any Socket Resources used below this statement. 
      mySocketPermissionUnion.Demand()
   End If
   
   Dim myIpHostEntry As IPHostEntry = Dns.Resolve("www.contoso.com")
   Dim myLocalEndPoint As New IPEndPoint(myIpHostEntry.AddressList(0), 11000)
   
   Dim s As New Socket(myLocalEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
   Try
      s.Connect(myLocalEndPoint)
   Catch e As Exception
      Console.WriteLine(("Exception Thrown: " + e.ToString()))
   End Try
   
   ' Perform all socket operations in here.
   s.Close()
End Sub

설명

SocketPermission 인스턴스 연결을 수락 하거나 시작할 수 있는 권한을 제어 Socket 연결 합니다.SocketPermission instances control permission to accept connections or initiate Socket connections. Socket 호스트 이름 또는 IP 주소, 포트 번호를 및 전송 프로토콜에 대 한 권한을 설정할 수 있습니다.A Socket permission can be established for a host name or IP address, a port number, and a transport protocol.

참고

이러한 이름은 IP 주소로 확인할 수 있고이 스택의 차단할 수 있습니다 하는 대로 호스트 이름을 사용 하 여 소켓 사용 권한 만들지 마세요.Avoid creating socket permissions using host names, as these names have to be resolved to IP addresses, and this might block the stack.

생성자

SocketPermission(NetworkAccess, TransportType, String, Int32) SocketPermission(NetworkAccess, TransportType, String, Int32) SocketPermission(NetworkAccess, TransportType, String, Int32) SocketPermission(NetworkAccess, TransportType, String, Int32)

지정된 사용 권한을 사용하여 주어진 전송 주소에 대한 SocketPermission 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the SocketPermission class for the given transport address with the specified permission.

SocketPermission(PermissionState) SocketPermission(PermissionState) SocketPermission(PermissionState) SocketPermission(PermissionState)

SocketPermission에 대한 무제한 액세스를 허용하거나 Socket에 대한 액세스를 허용하지 않는 Socket 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the SocketPermission class that allows unrestricted access to the Socket or disallows access to the Socket.

필드

AllPorts AllPorts AllPorts AllPorts

모든 포트를 나타내는 상수를 정의합니다.Defines a constant that represents all ports.

속성

AcceptList AcceptList AcceptList AcceptList

이 사용 권한 인스턴스 하에서 허용할 수 있는 엔드포인트를 식별하는 EndpointPermission 인스턴스 목록을 가져옵니다.Gets a list of EndpointPermission instances that identifies the endpoints that can be accepted under this permission instance.

ConnectList ConnectList ConnectList ConnectList

이 사용 권한 인스턴스 하에서 연결할 수 있는 엔드포인트를 식별하는 EndpointPermission 인스턴스 목록을 가져옵니다.Gets a list of EndpointPermission instances that identifies the endpoints that can be connected to under this permission instance.

메서드

AddPermission(NetworkAccess, TransportType, String, Int32) AddPermission(NetworkAccess, TransportType, String, Int32) AddPermission(NetworkAccess, TransportType, String, Int32) AddPermission(NetworkAccess, TransportType, String, Int32)

전송 주소에 대한 사용 권한 집합에 사용 권한을 추가합니다.Adds a permission to the set of permissions for a transport address.

Assert() Assert() Assert() Assert()

스택의 상위 호출자에게 리소스에 액세스할 수 있는 권한이 부여되지 않더라도 호출 코드가 이 메서드를 호출하는 코드를 통해 사용 권한 요구로 보호되는 리소스에 액세스할 수 있음을 선언합니다.Declares that the calling code can access the resource protected by a permission demand through the code that calls this method, even if callers higher in the stack have not been granted permission to access the resource. Assert()를 사용하면 보안 문제가 발생할 수 있습니다.Using Assert() can create security issues.

(Inherited from CodeAccessPermission)
Copy() Copy() Copy() Copy()

SocketPermission 인스턴스의 복사본을 만듭니다.Creates a copy of a SocketPermission instance.

Demand() Demand() Demand() Demand()

현재 인스턴스에서 지정한 사용 권한이 호출 스택의 일부 상위 호출자에만 부여된 경우 런타임에 SecurityException을 강제로 발생시킵니다.Forces a SecurityException at run time if all callers higher in the call stack have not been granted the permission specified by the current instance.

(Inherited from CodeAccessPermission)
Deny() Deny() Deny() Deny()

호출 스택의 상위 호출자가 이 메서드를 호출하는 코드를 통해 현재 인스턴스에서 지정한 리소스에 액세스하지 못하게 합니다.Prevents callers higher in the call stack from using the code that calls this method to access the resource specified by the current instance.

(Inherited from CodeAccessPermission)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

지정한 CodeAccessPermission 개체가 현재 CodeAccessPermission과 같은지 여부를 확인합니다.Determines whether the specified CodeAccessPermission object is equal to the current CodeAccessPermission.

(Inherited from CodeAccessPermission)
FromXml(SecurityElement) FromXml(SecurityElement) FromXml(SecurityElement) FromXml(SecurityElement)

SocketPermission 인스턴스를 XML 인코딩으로 다시 만듭니다.Reconstructs a SocketPermission instance for an XML encoding.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

해시 알고리즘과 해시 테이블 같은 데이터 구조에 사용하기 적합한 CodeAccessPermission 개체에 대한 해시 코드를 가져옵니다.Gets a hash code for the CodeAccessPermission object that is suitable for use in hashing algorithms and data structures such as a hash table.

(Inherited from CodeAccessPermission)
GetType() GetType() GetType() GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(Inherited from Object)
Intersect(IPermission) Intersect(IPermission) Intersect(IPermission) Intersect(IPermission)

SocketPermission 인스턴스의 논리 교집합을 반환합니다.Returns the logical intersection between two SocketPermission instances.

IsSubsetOf(IPermission) IsSubsetOf(IPermission) IsSubsetOf(IPermission) IsSubsetOf(IPermission)

현재 사용 권한이 지정된 사용 권한의 하위 집합인지 여부를 확인합니다.Determines if the current permission is a subset of the specified permission.

IsUnrestricted() IsUnrestricted() IsUnrestricted() IsUnrestricted()

개체의 전체 사용 권한 상태를 확인합니다.Checks the overall permission state of the object.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(Inherited from Object)
PermitOnly() PermitOnly() PermitOnly() PermitOnly()

호출 스택의 상위 호출자가 이 메서드를 호출하는 코드를 통해 현재 인스턴스에서 지정한 리소스를 제외한 모든 리소스에 액세스할 수 없게 합니다.Prevents callers higher in the call stack from using the code that calls this method to access all resources except for the resource specified by the current instance.

(Inherited from CodeAccessPermission)
ToString() ToString() ToString() ToString()

현재 권한 개체의 문자열 표현을 만들고 반환합니다.Creates and returns a string representation of the current permission object.

(Inherited from CodeAccessPermission)
ToXml() ToXml() ToXml() ToXml()

SocketPermission 인스턴스 및 현재 상태의 XML 인코딩을 만듭니다.Creates an XML encoding of a SocketPermission instance and its current state.

Union(IPermission) Union(IPermission) Union(IPermission) Union(IPermission)

SocketPermission 인스턴스의 논리합을 반환합니다.Returns the logical union between two SocketPermission instances.

명시적 인터페이스 구현

IPermission.Demand() IPermission.Demand() IPermission.Demand() IPermission.Demand() Inherited from CodeAccessPermission
IStackWalk.Assert() IStackWalk.Assert() IStackWalk.Assert() IStackWalk.Assert() Inherited from CodeAccessPermission
IStackWalk.Demand() IStackWalk.Demand() IStackWalk.Demand() IStackWalk.Demand() Inherited from CodeAccessPermission
IStackWalk.Deny() IStackWalk.Deny() IStackWalk.Deny() IStackWalk.Deny() Inherited from CodeAccessPermission
IStackWalk.PermitOnly() IStackWalk.PermitOnly() IStackWalk.PermitOnly() IStackWalk.PermitOnly() Inherited from CodeAccessPermission

적용 대상