(0) exportieren Drucken
Alle erweitern

ProtectionLevel-Enumeration

Aktualisiert: November 2007

Gibt die für einen authentifizierten Stream angeforderten Sicherheitsdienste an.

Namespace:  System.Net.Security
Assembly:  System (in System.dll)

public enum ProtectionLevel
public enum ProtectionLevel
public enum ProtectionLevel

MembernameBeschreibung
NoneNur Authentifizierung.
SignSigniert Daten, um die Integrität übertragener Daten sicherzustellen.
EncryptAndSignVerschlüsselt und signiert Daten, um die Vertraulichkeit und Integrität übertragener Daten sicherzustellen.

Diese Enumeration wird von der NegotiateStream-Klasse verwendet.

Hinweis zu Windows 95, Windows 98, Windows 98 Zweite Ausgabe, Windows Millennium Edition:

Nur None wird unterstützt.

Im folgenden Codebeispiel wird das Erstellen und Verwenden eines clientseitigen NegotiateStream veranschaulicht.

using System;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Principal;

namespace Examples.NegotiateStreamExample
{
    public class SynchronousAuthenticatingTcpClient 
    {
        public static void Main(String[] args)  
        {
            // Establish the remote endpoint for the socket.
            // For this example, use the local machine.
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            // Client and server use port 11000. 
            IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
            // Create a TCP/IP socket.
           TcpClient client = new TcpClient();
            // Connect the socket to the remote endpoint.
            client.Connect(remoteEP);
            Console.WriteLine("Client connected to {0}.",
                remoteEP.ToString());
            // Ensure the client does not close when there is 
            // still data to be sent to the server.
            client.LingerState = (new LingerOption(true,0));
            // Request authentication.
            NetworkStream clientStream = client.GetStream();
            NegotiateStream authStream = new NegotiateStream(clientStream); 
            // Request authentication for the client only (no mutual authentication).
            // Authenicate using the client's default credetials.
            // Permit the server to impersonate the client to access resources on the server only.
            // Request that data be transmitted using encryption and data signing.
            authStream.AuthenticateAsClient(
                 (NetworkCredential) CredentialCache.DefaultCredentials, 
                 "",
                 ProtectionLevel.EncryptAndSign,
                 TokenImpersonationLevel.Impersonation);
            DisplayAuthenticationProperties(authStream);
            DisplayStreamProperties(authStream);
            if (authStream.CanWrite)
            {
                 // Encode the test data into a byte array.
                byte[] message = System.Text.Encoding.UTF8.GetBytes("Hello from the client.");
                authStream.Write(message, 0, message.Length);
          authStream.Flush();
                Console.WriteLine("Sent {0} bytes.", message.Length);
         }
         // Close the client connection.
            authStream.Close();
            Console.WriteLine("Client closed.");

    }
         static void DisplayStreamProperties(NegotiateStream stream)
        {
             Console.WriteLine("Can read: {0}", stream.CanRead);
             Console.WriteLine("Can write: {0}", stream.CanWrite);
             Console.WriteLine("Can seek: {0}", stream.CanSeek);
             try 
             {
                 // If the underlying stream supports it, display the length.
                 Console.WriteLine("Length: {0}", stream.Length);
             } catch (NotSupportedException)
             {
                     Console.WriteLine("Cannot get the length of the underlying stream.");
             }

             if (stream.CanTimeout)
             {
                 Console.WriteLine("Read time-out: {0}", stream.ReadTimeout);
                 Console.WriteLine("Write time-out: {0}", stream.WriteTimeout);
             }
        }
         static void DisplayAuthenticationProperties(NegotiateStream stream)
        {
             Console.WriteLine("IsAuthenticated: {0}", stream.IsAuthenticated);
            Console.WriteLine("IsMutuallyAuthenticated: {0}", stream.IsMutuallyAuthenticated);
            Console.WriteLine("IsEncrypted: {0}", stream.IsEncrypted);
            Console.WriteLine("IsSigned: {0}", stream.IsSigned);
            Console.WriteLine("ImpersonationLevel: {0}", stream.ImpersonationLevel);
            Console.WriteLine("IsServer: {0}", stream.IsServer);
        }
    }
}


package Examples.NegotiateStreamExample; 
import System.*;
import System.Net.*;
import System.Net.Security.*;
import System.Net.Sockets.*;
import System.Security.Principal.*;

public class SynchronousAuthenticatingTcpClient
{
    public static void main(String[] args)
    {
        // Establish the remote endpoint for the socket.
        // For this example, use the local machine.
        IPHostEntry ipHostInfo = (IPHostEntry)Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress 
            = (IPAddress)ipHostInfo.get_AddressList().get_Item(0);
        // Client and server use port 11000. 
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
        // Create a TCP/IP socket.
        TcpClient client = new TcpClient();
        // Connect the socket to the remote endpoint.
        client.Connect(remoteEP);
        Console.WriteLine("Client connected to {0}.", remoteEP.ToString());
        // Ensure the client does not close when there is 
        // still data to be sent to the server.
        client.set_LingerState(new LingerOption(true, 0));
        // Request authentication.
        NetworkStream clientStream = client.GetStream();
        NegotiateStream authStream = new NegotiateStream(clientStream);
        // Request authentication for the client only (no mutual authentication).
        // Authenicate using the client's default credetials.
        // Permit the server to impersonate the client to access resources 
        // on the server only.
        // Request that data be transmitted using encryption and data signing.
        authStream.AuthenticateAsClient((NetworkCredential)(CredentialCache.
            get_DefaultCredentials()),
            "", 
            ProtectionLevel.EncryptAndSign,
            TokenImpersonationLevel.Impersonation);
        DisplayAuthenticationProperties(authStream);
        DisplayStreamProperties(authStream);
        if (authStream.get_CanWrite()) {
            // Encode the test data into a byte array.
            ubyte message[] = System.Text.Encoding.get_UTF8().GetBytes(
                "Hello from the client.");
            authStream.Write(message, 0, message.get_Length());
            authStream.Flush();
            Console.WriteLine("Sent {0} bytes.", 
                System.Convert.ToString(message.get_Length()));
        }
        // Close the client connection.
        authStream.Close();
        Console.WriteLine("Client closed.");
    } //main

    static void DisplayStreamProperties(NegotiateStream stream)
    {
        Console.WriteLine("Can read: {0}", 
            System.Convert.ToString(stream.get_CanRead()));
        Console.WriteLine("Can write: {0}", 
            System.Convert.ToString(stream.get_CanWrite()));
        Console.WriteLine("Can seek: {0}", 
            System.Convert.ToString(stream.get_CanSeek()));
        try {
            // If the underlying stream supports it, display the length.
            Console.WriteLine("Length: {0}", 
                System.Convert.ToString(stream.get_Length()));
        }
        catch (NotSupportedException exp) {
            Console.WriteLine("Cannot get the length of the underlying stream.");
        }

        if (stream.get_CanTimeout()) {
            Console.WriteLine("Read time-out: {0}", 
                System.Convert.ToString(stream.get_ReadTimeout()));
            Console.WriteLine("Write time-out: {0}", 
                System.Convert.ToString(stream.get_WriteTimeout()));
        }
    } //DisplayStreamProperties

    static void DisplayAuthenticationProperties(NegotiateStream stream)
    {
        Console.WriteLine("IsAuthenticated: {0}", 
            System.Convert.ToString(stream.get_IsAuthenticated()));
        Console.WriteLine("IsMutuallyAuthenticated: {0}", 
            System.Convert.ToString(stream.get_IsMutuallyAuthenticated()));
        Console.WriteLine("IsEncrypted: {0}", 
            System.Convert.ToString(stream.get_IsEncrypted()));
        Console.WriteLine("IsSigned: {0}", 
            System.Convert.ToString(stream.get_IsSigned()));
        Console.WriteLine("ImpersonationLevel: {0}", 
            System.Convert.ToString(stream.get_ImpersonationLevel()));
        Console.WriteLine("IsServer: {0}", 
            System.Convert.ToString(stream.get_IsServer()));
    } //DisplayAuthenticationProperties
} //SynchronousAuthenticatingTcpClient 


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0

Community-Beiträge

HINZUFÜGEN
Anzeigen:
© 2014 Microsoft