本文整理匯總了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();
}
}
}