Aracılığıyla paylaş


OperationBehaviorAttribute.Impersonation Özellik

Tanım

İşlemin desteklediği çağıranın kimliğe bürünme düzeyini gösteren bir değer alır veya ayarlar.

public:
 property System::ServiceModel::ImpersonationOption Impersonation { System::ServiceModel::ImpersonationOption get(); void set(System::ServiceModel::ImpersonationOption value); };
public System.ServiceModel.ImpersonationOption Impersonation { get; set; }
member this.Impersonation : System.ServiceModel.ImpersonationOption with get, set
Public Property Impersonation As ImpersonationOption

Özellik Değeri

Değerlerden ImpersonationOption biri. Varsayılan değer: NotAllowed.

Örnekler

Aşağıdaki hizmet kodu örneği, özelliğini Requiredolarak ayarlayarak Impersonation kimliğe bürünme gerektirir.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name="SampleHello",
    Namespace="http://microsoft.wcf.documentation"
  )]
  public interface IHello
  {
    [OperationContract]
    string Hello(string greeting);
  }

  public class HelloService : IHello
  {

    public HelloService()
    {
      Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
    }

    ~HelloService()
    {
      Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
    }

    [OperationBehavior(Impersonation=ImpersonationOption.Required)]
    public string Hello(string greeting)
    {
      Console.WriteLine("Called by: " + Thread.CurrentPrincipal.Identity.Name);
      Console.WriteLine("IsAuthenticated: " + Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString());
      Console.WriteLine("AuthenticationType: " + Thread.CurrentPrincipal.Identity.AuthenticationType.ToString());

      Console.WriteLine("Caller sent: " + greeting);
      Console.WriteLine("Sending back: Hi, " + Thread.CurrentPrincipal.Identity.Name);
      return "Hi, " + Thread.CurrentPrincipal.Identity.Name;
    }
  }
}
Imports System.ServiceModel
Imports System.Threading

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Name:="SampleHello", Namespace:="http://microsoft.wcf.documentation")> _
  Public Interface IHello
    <OperationContract> _
    Function Hello(ByVal greeting As String) As String
  End Interface

  Public Class HelloService
      Implements IHello

    Public Sub New()
      Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
    End Sub

    Protected Overrides Sub Finalize()
      Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
    End Sub

    <OperationBehavior(Impersonation:=ImpersonationOption.Required)> _
    Public Function Hello(ByVal greeting As String) As String Implements IHello.Hello
      Console.WriteLine("Called by: " & Thread.CurrentPrincipal.Identity.Name)
      Console.WriteLine("IsAuthenticated: " & Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString())
      Console.WriteLine("AuthenticationType: " & Thread.CurrentPrincipal.Identity.AuthenticationType.ToString())

      Console.WriteLine("Caller sent: " & greeting)
      Console.WriteLine("Sending back: Hi, " & Thread.CurrentPrincipal.Identity.Name)
      Return "Hi, " & Thread.CurrentPrincipal.Identity.Name
    End Function
  End Class
End Namespace

Aşağıdaki kod örneği, kimliğe bürünme için bu kimlik bilgilerini gerektiren işlemi çağırmadan önce istemci uygulaması kimlik bilgilerini ayarlamak için özelliğinin kullanımını ClientCredentials gösterir.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Security.Principal;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  public class Client
  {
    public void Run()
    {
      // Picks up configuration from the config file.
      SampleHelloClient wcfClient = new SampleHelloClient();
      try
      {
        // Set the client credentials to permit impersonation. You can do this programmatically or in the configuration file.
        wcfClient.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

        // Make calls using the proxy.
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        Console.WriteLine("Service returned: " + wcfClient.Hello(greeting));
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
        wcfClient.Close();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        wcfClient.Abort();
        Console.Read();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        wcfClient.Abort();
        Console.Read();
      }
    }
    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }
  }
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Security.Principal
Imports System.Threading

Namespace Microsoft.WCF.Documentation
  Public Class Client
    Public Sub Run()
      ' Picks up configuration from the config file.
      Dim wcfClient As New SampleHelloClient()
      Try
        ' Set the client credentials to permit impersonation. You can do this programmatically or in the configuration file.
        wcfClient.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation

        ' Make calls using the proxy.
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Enter a greeting to send and press ENTER: ")
        Console.Write(">>> ")
        Console.ForegroundColor = ConsoleColor.Green
                Dim greeting = Console.ReadLine()
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Called service with: " & vbCrLf & vbTab & greeting)
        Console.WriteLine("Service returned: " & wcfClient.Hello(greeting))
        Console.ForegroundColor = ConsoleColor.Blue
        Console.Write("Press ")
        Console.ForegroundColor = ConsoleColor.Red
        Console.Write("ENTER")
        Console.ForegroundColor = ConsoleColor.Blue
        Console.Write(" to exit...")
        Console.ReadLine()
        wcfClient.Close()
      Catch timeProblem As TimeoutException
        Console.WriteLine("The service operation timed out. " & timeProblem.Message)
        wcfClient.Abort()
        Console.Read()
      Catch commProblem As CommunicationException
        Console.WriteLine("There was a communication problem. " & commProblem.Message)
        wcfClient.Abort()
        Console.Read()
      End Try
    End Sub
    Public Shared Sub Main()
      Dim client As New Client()
      client.Run()
    End Sub
  End Class
End Namespace

Açıklamalar

Impersonation Belirtilen yöntemlerin (veya Requiredolarak ayarlanmış Allowed özellik ile işaretlenmiş olanlar) çağıranın Impersonation kimliği altında yürütülmesini sağlamak için özelliğini (kimliğe bürünme destekleyen bir bağlama yapılandırmasıyla birlikte) kullanın. özelliğiyle birlikte kullanılırken Allowed kimliğe bürünme işleminin ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations nasıl yapıldığı da dahil olmak üzere ayrıntılar için bkz. Temsilci Seçme ve Kimliğe Bürünme ve Nasıl yapılır: Hizmette İstemcinin Kimliğine Bürünme.

Not

Kimliğe bürünme gerçekleştiren bir hizmet uç noktasını program aracılığıyla eklerken, sözleşmeyi AddServiceEndpoint yeni System.ServiceModel.Description.ServiceDescription bir nesneye düzgün bir şekilde yüklemek için yöntemlerden birini veya ContractDescription.GetContract yöntemini kullanmanız gerekir. Yapılandırma dosyası kullanmak için fazladan bir adım gerekmez.

Kimliğe bürünme desteklenmediğinde belirli senaryolar olabilir. Daha fazla bilgi için bkz. Desteklenmeyen Senaryolar.

Şunlara uygulanır