【问题标题】:WCF Custom Authentication: Impersonate Windows Account for duration of requestWCF 自定义身份验证:在请求期间模拟 Windows 帐户
【发布时间】:2011-03-03 07:52:07
【问题描述】:

对于一个客户,我需要实现以下场景:

  • 用户帐户存储在数据库中。该列表包含 Windows 身份验证帐户和 SQL Server 身份验证帐户。密码不存储在数据库中(对于windows认证账户,windows系统用于验证凭证,SQL认证账户由SQL server验证)
  • 创建了一个 WCF 服务,用户使用自定义身份验证提供程序进行身份验证,以便能够处理 Windows 和 SQL 帐户。所以用户名(域\用户名或只是用户名)和密码都可以在服务中使用。
  • 已经有一个自定义 API,其中包含一个封装与数据库服务器的连接处理的类。如果是 Windows 帐户,我会为使用集成安全性的类提供一个连接字符串。如果是 SQL 帐户,我会构建一个连接字符串,其中包含 SQL 帐户的用户名和密码。在请求开始时创建类的对象并在请求结束时处理它会很方便。
  • 为了使 Windows 帐户方案正常工作,我想在请求期间模拟 Windows 帐户。

我正在寻找的是一种在一个地方实现模拟而不是分别实现每个服务方法的方法。

如何在服务中实现这一点?

感谢您的帮助,

马库斯

【问题讨论】:

    标签: wcf authentication


    【解决方案1】:

    也许这个例子可以帮助你: 取自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);
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。我不是在每种方法中寻找解决此问题的方法,因为会有很多方法(尽管许多方法可以由代码生成器生成)。你知道更通用的方法吗,例如使用行为?
    • 如果不在我添加的链接中,那是不可能的。
    【解决方案2】:

    其实这个链接给出了答案: ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations

    片段:

    有关详细信息,包括将 Allowed 与 ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations 属性一起使用时如何执行模拟,请参阅使用 WCF 进行委派和模拟以及如何:模拟服务上的客户端。

    以下是文字链接。

    https://msdn.microsoft.com/en-us/library/system.servicemodel.operationbehaviorattribute.impersonation(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多