WCF自定义身份验证:模拟Windows帐户的要求
问题描述:
为客户持续时间,我需要实现以下情形:WCF自定义身份验证:模拟Windows帐户的要求
- 用户帐户存储在数据库中。该列表包含Windows身份验证帐户和SQL Server身份验证帐户。密码不存储在数据库中(对于Windows验证的帐户,Windows系统用于验证凭证,SQL验证的帐户由SQL服务器验证)
- 创建WCF服务,用户使用自定义验证进行验证提供者为了能够处理Windows和SQL帐户。因此,服务中都提供了用户名(域\用户名或用户名)和密码。
- 已经有一个自定义API包含一个封装连接处理到数据库服务器的类。如果是Windows帐户,我会为使用集成安全性的类提供连接字符串。如果是SQL帐户,我会建立一个连接字符串,其中包含SQL帐户的用户名和密码。在请求开始时创建类的对象并在请求结束时处理它会很方便。
- 要使Windows帐户方案正常工作,我想在请求期间模拟Windows帐户。
我在寻找的是一种在一个地方而不是每个服务方法分别实现模拟的方法。
我该如何在服务中实现这一点?
感谢您的帮助,
马库斯
答
也许这个例子可以让你在路上:从here
public class HelloService : IHelloService
{
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string Hello(string message)
{
WindowsIdentity callerWindowsIdentity = ServiceSecurityContext.Current.WindowsIdentity;
if (callerWindowsIdentity == null)
{
throw new InvalidOperationException
("The caller cannot be mapped to a Windows identity.");
}
using (callerWindowsIdentity.Impersonate())
{
EndpointAddress backendServiceAddress = new EndpointAddress("http://localhost:8000/ChannelApp");
// Any binding that performs Windows authentication of the client can be used.
ChannelFactory<IHelloService> channelFactory = new ChannelFactory<IHelloService>(new NetTcpBinding(), backendServiceAddress);
IHelloService channel = channelFactory.CreateChannel();
return channel.Hello(message);
}
}
}
答
采取 其实这个链接给出了答案: ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations
A片段:
有关详细信息,包括在与ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations属性允许使用起来是如何进行模拟,看到代表团和模拟与WCF和如何在服务模拟客户端。
下面是文字链接。
感谢您的回答。我不想在每种方法中解决这个问题,因为会有很多方法(尽管许多方法可以由代码生成器生成)。你知道一个更一般的方法,例如使用行为? – Markus 2011-03-04 07:38:29
如果它不在我添加的链接中,那是不可能的。 – 2011-03-04 14:09:48