0

这是“看起来很容易”的任务,我自己无法完成。我有一个 WCF 服务,它应该受到用户名和密码凭据的保护并保持会话模式。

所以,这里是带有激活功能的界面部分。

<ServiceContract(SessionMode:=SessionMode.Required, ProtectionLevel:= ProtectionLevel.EncryptAndSign)>
Public Interface ServiceS

  <OperationContract()>
  Function Activation(A As String) As String

End Interface

背景类

Public Class Service1
Implements ServiceS

  Function Activation(A As String) As String Implement ServiceS.Activation
    Return "Hello, " & A & "!"
  End Function

End Class

接下来是 web.config。

<services>
  <service name="MyServiceS.Service1" behaviorConfiguration="MyBehavior">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyBinding" contract="MyServiceS.ServiceS" />
  </service>
</services>

<bindings>
  <wsHttpBinding>
    <binding name="MyBinding" useDefaultWebProxy="false">
      <security mode="Message" />
    </binding>
  </wsHttpBinding>
</bindings>

<behaviors>
  <serviceBehaviors>
    <behavior name="MyBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

<protocolMapping>
  <add scheme="https" binding="wsHttpBinding"/> 
  <add scheme="http" binding="wsHttpBinding"/> 
</protocolMapping>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

</system.serviceModel>

客户端代码。

Dim wcfTest As New MyService.ServiceSClient
wcfTest.ClientCredentials.Windows.ClientCredential.UserName = "Name"
wcfTest.ClientCredentials.Windows.ClientCredential.Password = "Password"
Dim Reply as String = wcfTest.Activation("Alex")

我期待什么?我需要我的 WPF 应用程序连接到我的服务并传递用户名和密码。如果它们是正确的,用户可以访问激活功能,如果不正确 - 会话应该被关闭。所以,我将登录名和密码传递给服务,但不知道如何检查它。它应该是界面或背景类的一部分吗?

我在互联网上看到了大量的例子,但其中大部分是关于其他事情的。

  • 推荐使用 ASP。我不需要 ASP。该服务应该是纯 WCF。
  • 使用证书。我不需要任何证书。它应该只是用户名和密码验证。
  • 角色。不需要角色。只有用户名和密码验证。
  • 基本的HttpBinding。不,只有 wsHttpBinding。
  • .Net 3.5 或 4.5。许多针对 .Net 3.5 的解决方案。我正在使用 4.0(以前使用 4.5,但已经集成了新技术,实际上没有完整的示例)。

如果您知道如何通过仅使用 wsHttpBinding 仅使用 WCF 仅使用 4.0 仅检查用户名和密码来保持会话,请提供建议。或者说,如果是这样,为什么不可能。非常感谢!

4

1 回答 1

0

您应该首先将客户端凭据类型设置为 UserName,如下所示。

    <wsHttpBinding>
      <binding name="Binding1">
        <security mode="Message">
          <message clientCredentialType="UserName" />
        </security>
      </binding>        
    </wsHttpBinding>

然后您可以像本文中那样实现自定义用户名和密码验证器并实现身份验证。

于 2013-08-18T17:17:14.473 回答