【问题标题】:How to consume a WCF Rest service with TransportClientCredentialsOnly security mode?如何使用 TransportClientCredentialsOnly 安全模式使用 WCF Rest 服务?
【发布时间】:2014-10-08 09:46:34
【问题描述】:

我花了一天时间弄清楚如何使用 TransactionCredentialOnly 安全模式消费一个 wcf rest 服务,客户端类型和代理类型是Windows

但我错过了一些我不知道的东西。

服务主机和调用代码在同一台机器上(当然我在调试) 这是回复Unauthorized。我预计这是成功状态,但事实并非如此。

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  X-SourceFiles: =?UTF-8?B?QzpcU2ltdWxhdG9yXEJyb2tlcmFnZUZpcm1cUHJvZmlsZVNlcnZpY2Uuc3ZjXGdyb3Vwcw==?=
  Cache-Control: private
  Server: Microsoft-IIS/8.0
  WWW-Authenticate: Negotiate
  WWW-Authenticate: NTLM
  X-Powered-By: ASP.NET
  Date: Wed, 08 Oct 2014 09:41:53 GMT
  Content-Length: 6088
  Content-Type: text/html; charset=utf-8
}

我有一个带有方法的 WCF Rest 服务:

[ServiceContract]
public class IProfileService
{

    [OperationContract]
    [WebGet(UriTemplate = "groups")]
    IList<Group> ListGroups();
}

Web.config

<webHttpBinding>
    <binding name="WebHttpBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="DefaultBehavior" name="MyNamespace.ProfileService">
    <endpoint address="mex" binding="mexHttpBinding" name="" contract="IMetadataExchange" />
    <endpoint behaviorConfiguration="RestFulBehavior" binding="webHttpBinding" bindingConfiguration="WebHttpBinding" name="ProfileServiceRestEndpoint" bindingName="ProfileServiceRestBinding" contract="MyNamespace.IProfileService" />
  </service>


<behaviors>
  <endpointBehaviors>
    <behavior name="RestFulBehavior">
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceAuthorization principalPermissionMode="None" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

还有控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = client.GetAsync("http://localhost:3522/ProfileService.svc/groups").Result;

            Console.WriteLine(response);
            Console.ReadLine();
        }
    }
}

WCF 项目的 IIS 配置

Always Start When Debugging: True
Anonymous Authentication: Enabled
Managed Pipeline Mode: Intergrated
SSL Enabled: True
SSL URL: localhost:44300
URL: localhost:3522
Windows Authentication: Enabled

【问题讨论】:

    标签: c# web-services wcf rest


    【解决方案1】:

    我认为此服务尚未启用 Windows 身份验证模式。您可以尝试在 IIS 中启用 Windows 身份验证。

    如果服务在 IIS express 上运行,则更改 applicationhost.config(位于 ..\Documents\IIS Express\config)以启用 windows 身份验证。

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2015-01-31
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多