2017-09-20 5 views
0

カスタムユーザー名バリデーターを使用して新しいAzureホストWCFサービスを構成しました。バリデータークラスは、Azureデータベース内の既存のaspnetUsersテーブルからユーザー名とパスワードを検証します。WCFサービスTransportWithMessageCredentialsカスタマーバリデータ

私は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"); 

おかげ

+0

[パスワードをHTTPSで送信する前にハッシュする必要がありますか?](https://stackoverflow.com/questions/5411892/is-it-necessary-to-hash-passwords-before-sending-them) -over-https) – MickyD

答えて

0

をだから私の質問は本当に、十分に安全なこのように、ユーザ名とパスワードを送信していますか?

はい。 HTTPSによって送信されるものはすべて暗号化されるため、手動で暗号化せずにユーザー名とパスワードを送信できます。私の意見では、サービスにリクエストを送信するたびにユーザー名とパスワードを送信することはお勧めしません。最初にトークンを生成し、そのトークンを使用して一定期間のリクエストを検証することをお勧めします。

+0

ありがとうAmor - 私はトークンの生成を調べます。 – Hustler101