【问题标题】:WCF Service TransportWithMessageCredentials Customer validatorWCF 服务 TransportWithMessageCredential 自定义验证器
【发布时间】:2018-03-01 08:06:49
【问题描述】:

我已经使用自定义用户名验证器配置了一个新的 Azure 托管 WCF 服务。验证器类将验证 Azure 数据库中现有 aspnetUsers 表中的用户名和密码。

我已使用 TransportWithMessageCredentials 绑定配置服务,因此客户端将在请求中以明文形式提供其用户名和密码。

然后我的代码将查找用户并从数据库中获取他们的哈希密码,然后使用它对通过服务发送的密码进行哈希处理。如果它们匹配,则允许请求。

为了验证我正在使用此代码的密码。

 public static bool checkPassword(string hashedPassword, string password)
    {

        byte[] buffer4;
        if (hashedPassword == null)
        {
            return false;
        }
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        byte[] src = Convert.FromBase64String(hashedPassword);
        if ((src.Length != 0x31) || (src[0] != 0))
        {
            return false;
        }
        byte[] dst = new byte[0x10];
        Buffer.BlockCopy(src, 1, dst, 0, 0x10);
        byte[] buffer3 = new byte[0x20];
        Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
        using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
        {
            buffer4 = bytes.GetBytes(0x20);
        }
        return ByteArraysEqual(buffer3, buffer4);
    }

所以我的问题是,以这种方式发送用户名和密码是否足够安全?由于一切都在通过 https,我假设它是但希望得到一些指导,因为我对一般安全性相当陌生。

该服务也将受到 IP 限制。

这是我的服务模型配置。

 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyValidatorClass,MyNameSpace" />
          </serviceCredentials>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="HttpBinding" maxReceivedMessageSize="2097152" receiveTimeout="00:02:00" sendTimeout="00:02:00">
        </binding>
        <binding name="HttpsBinding" maxReceivedMessageSize="2097152" receiveTimeout="00:02:00" sendTimeout="00:02:00">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="MyService" behaviorConfiguration="MyServiceBehaviour">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="HttpsBinding" contract="MyContract" />
        <host>
          <baseAddresses>
            <add baseAddress="https://MyServiceInAzure.net/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

这是客户将使用的:

Client.Service call = new Client.ServiceClient();
        call.ClientCredentials.UserName.UserName = "MyUsername";
        call.ClientCredentials.UserName.Password = "MyPassword";

        var result = call.PostCall("Hello World");

谢谢

【问题讨论】:

标签: c# wcf azure wcf-security


【解决方案1】:

所以我的问题是,以这种方式发送用户名和密码是否足够安全?

是的。由于通过 HTTPS 发送的所有内容都会被加密,因此您可以在不手动加密的情况下发送用户名和密码。在我看来,不建议每次向您的服务发送请求时都发送用户名和密码。建议您在第一时间生成一个token,并使用token在一段时间内验证您的请求。

【讨论】:

  • 谢谢 Amor - 我会研究令牌生成。
猜你喜欢
  • 2010-12-06
  • 1970-01-01
  • 2015-05-24
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 1970-01-01
  • 2017-07-27
相关资源
最近更新 更多