【问题标题】:Can IIS-hosted WCF service be configured for BOTH Windows Auth and Anonymous?可以为 Windows Auth 和 Anonymous 配置 IIS 托管的 WCF 服务吗?
【发布时间】:2009-06-10 21:13:13
【问题描述】:

我有一个小型 WCF Web 服务,它与内置 WCF 服务主机一起工作,并由 Visual Studio 2008 内置开发 Web 服务器托管。

在这些托管环境中,我依赖 WCF 测试客户端来调用服务方法。

现在我的下一阶段测试遇到了问题:

我将它托管在我的 WinXP 开发机器上的 IIS 5.1 中,我认为问题可能是我无法继续使用 WCF 测试客户端了。这是发生了什么:

案例 1:“匿名访问”已检查(启用)

WCF 测试客户端 UI 正确显示,显示 WebMethods 和 INVOKE 按钮。 然而,当我单击 INVOKE 时,它无法连接到需要 Windows 身份验证的后端数据存储(第 3 方产品)。我可以发布从 product.DLL 返回的错误,但我认为它不相关。

案例 2:“匿名访问”未选中(禁用)

WCF 测试客户端 UI 甚至无法正确初始化。我对此的研究告诉我,MEX(WS-Metadata Exchange)需要“匿名访问”并且(显然)WCF 测试客户端需要 MEX。以下是返回错误的关键 sn-ps:

Error: Cannot obtain Metadata from http://localhost/wcfiishost
The remote server returned an error: (401) Unauthorized.HTTP GET Error
URI: http://localhost/wcfiishost    
There was an error downloading 'http://localhost/wcfiishost'.    
The request failed with the error message:
Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service

有很多关于绑定选项、消息安全等的解释以及我真的不明白的东西。以下是我对自己所处位置的看法,但我希望得到您的意见:

(a) 因为我知道我的 WCF Web 服务必须配置为使用 Windows 身份验证,所以我得出结论,在 IIS 中托管我的服务时,我无法继续使用 WCF 测试客户端。它实际上已经超过了它对我的用处。我只需要花时间编写一个 Web 客户端,因为如果没有 Anonymous,WCFTestClient 将无法工作。

(或)

(b) 如果 WCF 测试客户端和托管服务配置正确,则可以使用 WCF 测试客户端(我只是不知道有什么特殊的配置技术)。

哪个是正确的?是时候停止使用 WCFTestClient 了,还是有办法同时使用它?提前感谢您的建议。

编辑:09 年 6 月 11 日

我还能提供什么帮助其他人帮助我解决这个问题吗?

【问题讨论】:

  • PS:您的服务使用哪种绑定?您可以尝试使用“basicHttpBinding”(如果您还没有使用它)吗?

标签: wcf metadata kerberos mex wcftestclient


【解决方案1】:

我只是尝试使用相同的设置 - 但就我而言,一切似乎都运行良好。

  • ASP.NET 网站
  • WCF 服务,使用 basicHttpBinding,完全没有任何特殊设置
  • IIS 应用程序匿名 = 已启用且 Windows 身份验证 = 已启用(均已打开)

我可以通过 WcfTestClient 轻松连接到它并检索元数据,然后我可以调用它,没问题。

在我的服务函数中,我检查当前用户是否为已知用户,是否正确识别为 Windows 身份验证用户:

    ServiceSecurityContext ssc = ServiceSecurityContext.Current;

    if (ssc.IsAnonymous)
    {
        return "anonymous user";
    }
    else
    {
        if(ssc.WindowsIdentity != null)
        {
            return ssc.WindowsIdentity.Name;
        }

        if (ssc.PrimaryIdentity != null)
        {
            return ssc.PrimaryIdentity.Name;
        }
    }

    return "(no known user)";

我真的不知道,还有什么要检查的(除了我在使用 IIS7 的 Vista 上)。您是否有可能包含此代码来检查您的服务代码中的用户?只是为了看看......

马克

【讨论】:

    【解决方案2】:

    Marc,你的设置甚至比 Johns 还差。

    John 使用 WSHttpBinding,它使用 Windows 凭据进行消息模式传输。 Windows 身份验证未与 BasicHttpBinding 一起使用。此外,John 禁用了 AnonymousAuthentication,这就是元数据交换 (mex) 失败的原因。

    调用甚至不会到达服务端函数内部,因为我们在尝试调用时会收到错误 401(未经授权)。

    只知道约翰,我有同样的问题,我正在尝试以某种方式为每个端点设置单独的绑定。希望这会奏效。

    【讨论】:

    • 我们已经为 mex 使用了不同的绑定,所以不要介意这个想法。我现在也很茫然。
    【解决方案3】:

    当我设置了这个问题的标题/主题并在这里走到了死胡同时,我在 MSDN 论坛中打开了相同的问题,但标题的侧重点不同(问题的内容基本相同)。

    对我来说,真正的问题是如何在没有设置匿名身份验证的情况下在 IIS 中使用 WCFTestClient(因为我的服务只需要集成 Windows 身份验证)。

    Mex 显然需要匿名,默认情况下 WCFTestClient 似乎需要 Mex。关键似乎是为了适应我对 web.config 文件的仔细修改。

    无论如何,我在下面的 web.config 中使用它 (the MSDN link is here:

    <?xml version="1.0"?>
    <configuration>
    

                <endpoint address="" 
                            binding="wsHttpBinding"
                            bindingConfiguration="wsBindingConfig"
                            contract="sdkTrimFileServiceWCF.IFileService">
    
                                     <identity>
                                        <dns value="localhost" />
                                     </identity>
                </endpoint>
    
                <endpoint address="basic" 
                            binding="basicHttpBinding"
                            bindingConfiguration="bindingConfig" 
                            contract="sdkTrimFileServiceWCF.IFileService" />
            </service>
        </services>
    
        <bindings>
            <basicHttpBinding>
                <binding name="bindingConfig">
                    <security mode="TransportCredentialOnly">
                        <transport clientCredentialType="Windows"/>
                    </security>
                </binding>
            </basicHttpBinding>
    
            <wsHttpBinding>
                <binding name="wsBindingConfig">
                    <security mode="Transport">
                        <transport clientCredentialType="Windows"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
    

            </serviceBehaviors>
        </behaviors>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多