本文整理汇总了C#中System.ServiceModel.OperationBehaviorAttribute.Impersonation属性的典型用法代码示例。如果您正苦于以下问题:C# OperationBehaviorAttribute.Impersonation属性的具体用法?C# OperationBehaviorAttribute.Impersonation怎么用?C# OperationBehaviorAttribute.Impersonation使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.ServiceModel.OperationBehaviorAttribute
的用法示例。
在下文中一共展示了OperationBehaviorAttribute.Impersonation属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Hello
//引入命名空间
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;
}
}
}
示例2: Run
//引入命名空间
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();
}
}
}