【问题标题】:Authenticate user from WPF app across two web services (from WCF to WebApi)跨两个 Web 服务(从 WCF 到 WebApi)从 WPF 应用程序对用户进行身份验证
【发布时间】:2016-04-25 11:13:38
【问题描述】:

我有以下情况:

WPF client application -> WCF Service -> WebApi Service

我希望能够在 WebApi 服务中验证客户端应用程序的用户。我不需要完全的模拟/授权——我只需要客户的登录 ID,因为我正在使用第三方授权系统的用户 ID。 WCF Service 和 Web Api Service 在同一台服务器上。

我正在尝试的当前方法是模拟 WindowsIdentity,如下所示。

1.使用 Owin 自托管的 WebApi 服务

    WebApp.Start(options,
    builder =>
    {
        // Authenticate POST requests, but not GET requests
        // Adapted from: http://stackoverflow.com/questions/17457382/windows-authentication-with-signalr-and-owin-self-hosting
        var listener = (HttpListener)builder.Properties[typeof(HttpListener).FullName];
        listener.AuthenticationSchemes = 
            AuthenticationSchemes.Negotiate | AuthenticationSchemes.Anonymous;
        listener.AuthenticationSchemeSelectorDelegate =
            request => (request.HttpMethod == HttpMethod.Get.Method)
                ? AuthenticationSchemes.Anonymous
                : AuthenticationSchemes.Negotiate;

        // etc
    });

2。 WCF 服务

<system.web><authentication mode="Windows" />

<bindings>:

<binding name="BasicHttpEndpointBinding">
  <security mode="TransportCredentialOnly">
    <transport clientCredentialType="Windows" />
  </security>
</binding>

在我要模拟的特定功能的代码中:

WindowsIdentity callerIdentity = ServiceSecurityContext.Current.WindowsIdentity; 

using (callerIdentity.Impersonate())
{
    var request = ...;
    request.ImpersonationLevel = TokenImpersonationLevel.Impersonation;
    request.UseDefaultCredentials = true;

    // Use request to call Web Api
}

3.在客户端:

<endpointBehaviors>
  <behavior name="ImpersonationBehavior">
    <clientCredentials>
      <windows allowedImpersonationLevel="Impersonation" />
    </clientCredentials>
  </behavior>
</endpointBehaviors>

在我的本地 PC 上进行此设置后,我看到 WCF 服务尝试连接到 WebApi 服务时出现错误:

{"An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:9102"}

(请注意,使用 WindowsIdentity callerIdentity = WindowsIdentity.GetCurrent(); 而不是 WindowsIdentity callerIdentity = ServiceSecurityContext.Current.WindowsIdentity; 可以正常工作 - 尽管显然这是使用 WCF 用户而不是客户端应用程序用户)

我的问题 - 这是在 Web Api 服务中确定用户名的最明智的方法吗?甚至有可能这样吗?如果是这样,知道如何调试“禁止”错误并使此设置正常工作吗?

【问题讨论】:

    标签: c# asp.net-web-api owin windows-authentication wcf-security


    【解决方案1】:

    对于任何感兴趣的人,我设法通过更改以下内容来使其正常工作。

    1. 以管理员身份运行这两个服务。 (我相信通过正确的配置,这不是必需的 - 但这对我的目的来说已经足够了)

    2. 确保端口预留 URL 与 WebApi 启动参数中指定的 URL 完全匹配(参见:Self hosted OWIN and urlacl)。就我而言,我将以下内容用于WebApp.Start(...) 参数:

      string baseAddress = "http://+:9100";
      StartOptions options = new StartOptions();
      options.Urls.Add(baseAddress);
      

    netsh 如下:

    netsh http add urlacl url=http://+:9111/ user=EVERYONE
    
    1. 在 WCF 服务中,我想使用模拟的服务实现中的方法中添加了以下行为:

      [OperationBehavior(Impersonation = ImpersonationOption.Required)]
      

    ...对于本合同中的其他方法,我需要添加以下内容

        [OperationBehavior(Impersonation = ImpersonationOption.NotAllowed)]
    
    1. 我使用“localhost”而不是显式主机名从 WCF 服务引用了 WebApi 服务。不确定这是否是必需的。

    2. 运行 WCF 服务的用户需要具有使用模拟的权限(如果以管理员身份运行,则应该已经设置)。使用 gpedit.msc;导航到本地计算机策略 -> 计算机配置 -> Windows 设置 -> 安全设置 -> 本地策略 -> 用户权限分配;确保“身份验证后模拟客户端”包含您运行 WCF 服务的用户。 (取自:https://blogs.technet.microsoft.com/askperf/2007/10/16/wmi-troubleshooting-impersonation-rights/

    最后一步是计算出问题/这个答案所需的最少配置集......我想我会再等一天:-)

    【讨论】:

      猜你喜欢
      • 2010-11-08
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多