在WCF中启用不带SSL的基本HttpBinding

时间:2010-09-28 16:16:46

标签: c# wcf web-config

这里的想法是,对于我的测试,在我承诺购买SSL证书之前,我想在非ssl模式下启用WCF服务。我以前使用这段代码完成了它,但对于我的生活,无法弄清楚如何将它转换为web.config文件。

如果有人可以就如何进行此翻译向我指明正确的方向,那将非常感激。

                Binding basicBinding = null;
                if (RegistryConnectionStringFactory.UseSslForCommunications)
                {
                    basicBinding = new BasicHttpBinding();
                    (basicBinding as BasicHttpBinding).Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
                    (basicBinding as BasicHttpBinding).Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

                    creds.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.MembershipProvider;
                    creds.UserNameAuthentication.MembershipProvider = Membership.Provider;
                }
                else
                {
                    HttpTransportBindingElement transport = new HttpTransportBindingElement()
                    {
                        AuthenticationScheme = System.Net.AuthenticationSchemes.Basic
                    };
                    basicBinding = new CustomBinding(transport);

                    svcHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new AspNetUsernamePasswordValidator();
                    svcHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
                }

4 个答案:

答案 0 :(得分:2)

BasicHttpBinding的基于配置的方法有什么问题?您只需使用TransportWithMessageCredential和UserName凭据通过HTTPS或TransportCredentialOnly进行通信,使用基本凭据进行HTTP通信。

答案 1 :(得分:2)

这是一个极端但您可以通过将安全模式设置为“none”来允许匿名非SSL。匿名也可能会影响您的测试,所以可能不推荐。

<binding name="HttpBasicSecurityConfig">
  <security mode="None">
  </security>
</binding>

来自http://msdn.microsoft.com/en-us/library/ms731172.aspx

绑定中的传输凭据

Type   Description
None   Specifies that the client does not need to present any credential. This translates to an anonymous client.

答案 2 :(得分:1)

因为您的帖子建议您要在测试完成之前避免购买SSL证书,我想问:为了节省一些时间,您是否可以使用{{1}创建自己的自签名证书}?

如果是这样,这些说明可能会有所帮助。

创建根证书密钥文件...

makecert

创建.PFX文件......

makecert  -r -pe -n "CN=My Own Authority,O=My Company,C=US" -ss CA -sr CurrentUser -a sha1 -sky signature -sv mycert.pvk mycert.cer

然后,使用“证书”管理单元,将makecert -pe -n "CN=localhost" -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1 -ic mycert.cer -iv mycert.pvk -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -sv localhost.pvk localhost.cer pvk2pfx -pvk localhost.pvk -spc localhost.cer -pfx localhost.pfx 文件导入本地计算机上的“受信任的根证书颁发机构”,以告知在本地计算机上运行的应用程序,由您自己的权限签署的任何证书都是信任的。

接下来,将mycert.cer文件导入到本地计算机上的个人存储中。 (这样做会使证书可供IIS使用,以便它可以通过您自己的权限声明自己是名为“localhost”的服务器。)

这里详细描述了如何将.PFX文件导入IIS 7:http://www.digicert.com/ssl-support/pfx-import-export-iis-7.htm

答案 3 :(得分:0)

我今天也在看类似的东西,但我的知识并非100%完整。

对于绑定,您需要执行以下操作:

   <bindings>
  <customBinding>
    <binding name="CustomBinding">
      <httpTransport authenticationScheme="Basic"/>
    </binding>
  </customBinding>

然后,您需要为自定义验证创建serviceBehaviour:

        <behavior name="serviceBehavior" >
      <serviceAuthorization principalPermissionMode="UseAspNetRoles"roleProviderName="CustomRolesProvider" />
      <serviceCredentials>
        <userNameAuthentication customUserNamePasswordValidatorType ="AspNetUsernamePasswordValidator [Fully qualified name]" userNamePasswordValidationMode="Custom" />
      </serviceCredentials>

显然没有经过测试,但是类似的配置对我来说很有用,它可能会让你开始......

相关问题