调用使用Windows凭据保护的ASMX服务

bal*_*dre 4 web-services windows-authentication asp.net-mvc-4

我已经获得了一个Web服务(ASMX)来消耗我需要使用Windows凭据的女巫.

所以,我已经设置了我的客户端VPN并调用了WSDL,保存为XML文件,并使用,生成代理类svcutil.exe,到目前为止,这么好......

我把这项服务称为

// Web Service
client = new CmListSync.Models.WebCorePlayersSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(cUser, cPass, cDoma);
Run Code Online (Sandbox Code Playgroud)

web.config我有这个设置:

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WebCorePlayersSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="true" />
          <security mode="None">
            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Windows" algorithmSuite="Default" negotiateServiceCredential="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://vm-wssrv01/players.asmx" binding="wsHttpBinding"
        bindingConfiguration="WebCorePlayersSoap" contract="WebCorePlayersSoap"
        name="WebCorePlayersSoap" />
    </client>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试调用该服务时,我得到一个异常说:

HTTP请求未经授权,客户端身份验证方案为"匿名".从服务器收到的身份验证标头是'Basic realm = \"vm-wssrv01 \"'.

我错过了什么?因为我提供了Windows凭据,服务不应该正常进行身份验证吗?我还应该做些什么?

我尝试过的:

  • 安全模式设置Message和我同样的错误在上面的问题
  • 将安全模式设置TransportWithMessageCredentialI got:提供的URI方案'http'无效; 预期'https'.\ r \n参数名称:via
  • 将安全模式设置Transport,我得到:绑定验证失败,因为WSHttpBinding不支持基于传输安全性(HTTPS)的可靠会话.无法打开通道工厂或服务主机.使用消息安全性实现HTTP上的安全可靠消息传递

来自John Saunders的评论:

我已切换到 basicHttpBinding

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="WebCorePlayersSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="Windows" proxyCredentialType="Windows" realm="vm-wssrv01" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://vm-wssrv01/players.asmx" binding="basicHttpBinding"
        bindingConfiguration="WebCorePlayersSoap" contract="WebCorePlayersSoap"
        name="WebCorePlayersSoap" />
    </client>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

并尝试将security模式更改为:

  • TransportWithMessageCredential {"提供的URI方案'http'无效;预期'https'.\ r \n参数名称:via"}

  • TransportCredentialOnly {"HTTP请求未经授权,客户端身份验证方案'协商'.从服务器收到的身份验证标头是'Basic realm = \"vm-wssrv01 \"'."}

  • Message {"BasicHttp绑定要求BasicHttpBinding.Security.Message.ClientCredentialType等效于安全消息的BasicHttpMessageCredentialType.Certificate凭证类型.为UserName凭证选择Transport或TransportWithMessageCredential安全性."}

  • Transport {"提供的URI方案'http'无效;预期'https'.\ r \n参数名称:via"}

  • None {"HTTP请求未经授权,客户端身份验证方案为'匿名'.从服务器收到的身份验证标头为'Basic realm = \"vm-wssrv01 \"'."}

我的想法用完了:(该服务仅限HTTP,而非HTTPS,我没有使用证书......

bal*_*dre 5

3天后,在John Saunders的大力帮助下,他表示ASMX服务的唯一可能绑定是basicHttpBinding(我的搜索答案开始更加集中)我进入了这个:

在服务调用者中,必须使用client.ClientCredentials.UserNameas:

// Web Service
client = new CmListSync.Models.WebCorePlayersSoapClient();
client.ClientCredentials.UserName.UserName = cUser;
client.ClientCredentials.UserName.Password = cPass;
Run Code Online (Sandbox Code Playgroud)

在配置部分,需要使用:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="WebCorePlayersSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://vm-wssrv01/players.asmx" binding="basicHttpBinding"
        bindingConfiguration="WebCorePlayersSoap" contract="WebCorePlayersSoap"
        name="WebCorePlayersSoap">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
Run Code Online (Sandbox Code Playgroud)