OperationBehaviorAttribute.Impersonation 속성

정의

작업에서 지원하는 호출자 가장 수준을 나타내는 값을 가져오거나 설정합니다.

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

속성 값

ImpersonationOption

ImpersonationOption 값 중 하나입니다. 기본값은 NotAllowed입니다.

예제

다음 서비스 코드 예제에는 Impersonation 속성을 Required로 설정하는 방식을 통한 가장이 필요합니다.

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

다음 코드 예제에서는 가장에 클라이언트 애플리케이션 자격 증명이 필요한 작업에서 작업 호출에 앞서 자격 증명을 설정하기 위해 ClientCredentials 속성을 사용하는 것을 보여 줍니다.

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

설명

지정된 메서드, 즉 Impersonation 속성이 Impersonation 또는 Allowed로 설정되어 표시된 메서드가 호출자의 ID로 실행될 수 있게 하려면 가장을 지원하는 바인딩 구성과 함께 Required 속성을 사용합니다. 세부 정보를 사용 하는 경우 가장이 수행 하는 방법을 비롯 한 Allowed 와 함께 ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations 속성을 참조 하세요 위임 및 가장방법:서비스에서클라이언트가장.

참고

가장을 수행하는 서비스 엔드포인트를 프로그래밍 방식으로 추가할 때는 AddServiceEndpoint 메서드나 ContractDescription.GetContract 메서드 중 하나를 사용하여 계약을 새 System.ServiceModel.Description.ServiceDescription 개체에 제대로 로드해야 합니다. 구성 파일을 사용하기 위해 추가로 필요한 단계는 없습니다.

특정 시나리오에서는 가장이 지원되지 않을 수 있습니다. 자세한 내용은 지원 되지 않는 시나리오합니다.

적용 대상