通过 https 进行消息安全的 wcf userName 身份验证

wcf userName authentication with message security over https

情况是这样的,我希望许多客户端通过 https 使用用户名身份验证来调用我的 Web 服务。安全是第一要务,所以我正在考虑将 wshttpbinding 与消息安全性结合使用。虽然我不知道我的想法是否正确。 问题是我已经有了一些可用的东西,但我不知道它是否需要更改以实现更好的安全性。 这是目前完成的内容。

<services>
  <service name="myService" behaviorConfiguration="myBehavior" >
    <endpoint address="" binding="basicHttpBinding" contract="myIService" bindingConfiguration="RequestUserName_BasicHttp" >
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8080/myService/" />
      </baseAddresses>
    </host>
  </service>
</services>

<bindings>
  <basicHttpBinding>
    <binding name="RequestUserName_BasicHttp">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

 <serviceCredentials>
   <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="myvalidator, myNamespace"/>
 </serviceCredentials>

所以,通过这种方式(有效)我​​认为我没有最好的安全性(至少我需要通过 https 发送请求)。我可以做些什么来实现 better/best 安全?我尝试过使用 wshttpbinding 和 https,但我在证书方面遇到了一些问题。 开发环境为WindowsXP、VS2010、IIS7.5 express。 还有一个描述服务的 class 库和一个用于使用它的 consoleClient 应用程序...客户端有自己的 app.config 文件,其中包含凭据(用户名和密码)。

您已经实施了用户 ID 和密码验证,如果您想要执行消息的加密和解密,您必须使用带有 HttpsBinding 或 WsHttpBinding 的证书。有关 wcf 中身份验证和授权的更多信息,请阅读此 MSDN documentation

好的,我考虑了 Ramesh Babu 的回答,并对我的项目做了一点改动。 因此,我没有为 wcf 服务创建 class 库,而是创建了一个 WCF 服务应用程序(VS2010 中有此选项)。其他一切都保持不变,所以我创建了一个新的 Web.config 文件,如下所示

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="MyBinding">
                    <security mode="Message">
                        <message clientCredentialType="UserName"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <services>
            <service behaviorConfiguration="MyBehavior" name="myName">
                <endpoint address="myService.svc" binding="wsHttpBinding"
                          bindingConfiguration="MyBinding"
                          contract="myService.ImyService" />
                <endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:44400/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebugincludeExceptionDetailInFaults="true" />
                    <serviceCredentials>
                        <userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="myService.Authentication.CustomValidator, myService" />

                        <serviceCertificate
                            findValue="MyCertificate"
                            x509FindType="FindBySubjectName"
                            storeLocation="LocalMachine"
                            storeName="My" />

                    </serviceCredentials>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <directoryBrowse enabled="true"/>
    </system.webServer>

    <connectionStrings>
        <add name="myEntities" connectionString="......" />
    </connectionStrings>

</configuration>

所以我需要创建一个证书,我使用 SelfCert 创建一个并将其复制到 TrustedPeople(在 运行 中键入 mmc)。 在此之后,我创建了一个控制台应用程序来使用 thw 服务,并且 app.config 应用程序文件是自动构建的。