wcf - 使用 WCF 连接到使用用户名/密码进行 Web 服务身份验证

标签 wcf wcf-security wcf-authentication

我使用 Visual Studio 2008 创建了 Web 服务的代理,它在 app.config 中为我创建了以下条目:

<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MyNameHandlerSoapBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://www.***/***/***"
              binding="basicHttpBinding" bindingConfiguration="MyNameHandlerSoapBinding"
              contract="***.MyNameHandler" name="MyName">
          </endpoint>
        </client>
    </system.serviceModel>

网络服务具有用户名/密码身份验证,因此我需要将其添加到此处。

我在 WCF 文档的海洋中有点迷失,我想我必须从 basicHttpBinding 更改为 wsHttpBinding 或 customBinding 才能添加身份验证元素,但我不太理解它。有人可以提供任何快速提示或任何有用的链接来说明如何执行此操作吗?

编辑:

我将安全部分更改为:

<security mode="Transport">
    <transport clientCredentialType="Basic" proxyCredentialType="None"
         realm="" />
</security>

并在代码中添加:

ws.ClientCredentials.UserName.UserName = "";
ws.ClientCredentials.UserName.Password = "";

现在看来它可能正在使用凭据,但它给了我错误:

提供的 URI 方案“http”是无效的 URI,应为“https”

我什至不知道这是否是正确的方法......

最佳答案

我在这里发布了供 future 读者使用的解决方案:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="MyHandlerSoapBinding" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <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://www.***/***/***/MyHandler"
          binding="basicHttpBinding" bindingConfiguration="MyHandlerSoapBinding"
          contract="***.MyHandler" name="MyHandler">
      </endpoint>

    </client>
  </system.serviceModel>

最后我可以使用默认的 basicHttpBinding。与问题中发布的代码的唯一区别是安全节点

另请注意 mode="TransportCredentialOnly" 选项,这允许您使用 http 而不是 https 发送用户名/密码。这对于测试我正在使用的环境是必要的。稍后,显然您会更喜欢使用 https 发送您的凭据。

然后在代码中输入您的用户名/密码:

var ws = new ***.MyHandlerClient("MyHandler");
ws.ClientCredentials.UserName.UserName = "myUsername";
ws.ClientCredentials.UserName.Password = "myPassword";
var result = ws.executeMyMethod();

关于wcf - 使用 WCF 连接到使用用户名/密码进行 Web 服务身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/806007/

相关文章:

c# - 在 WCF 中获取调用者的主机名

.net - 将wsHttpBinding SSL传输安全性与消息安全性结合使用有什么好处?

wcf - 在互联网上发布的wcf服务有 'You have created a service'页面是不是很糟糕?

WCF - 为什么 'CustomAuthorizationPolicy' 在每个操作上都会创建一个 'CustomPrincipal'?

wcf - 在Mac Os X的Mono控制台应用程序中托管WCF服务

asp.net-mvc - 处置 WCF 代理的正确方法是什么?

c# - 来自 MVC 应用程序的 WCF 服务身份验证共享相同的成员资格提供者

c# - WCF:将用户名和密码传递给另一项服务(无 Https)

c# - 从 MVC 客户端调用 MS WCF REST 服务 POST BAD 请求 c#

c# - 异步WCF : wait for another call