2013-11-27 25 views
9

私は非常に基本的ではあるが安全なユーザー名/パスワード認証をwcfで実行しようとしています。WCFの基本認証

しかし、ServiceSecurityContext.Current.PrimaryIdentity;の値を見ると、Windowsマシンの資格情報が含まれており、サービスに提供したユーザー名とパスワードの代わりに(まだ承認されていなくても)承認されていると主張しています。

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- 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="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WsHttpBindingConfig"> 
      <security mode="TransportWithMessageCredential"> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 

    </bindings> 
    <protocolMapping> 
     <add binding="wsHttpBinding" scheme="http" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

を次のようにサービスの私のweb.configファイルがあると

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_IService1" /> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost/WcfSecuredService/Service1.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" 
       contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> 
      </endpoint> 
     </client> 
    </system.serviceModel> 
</configuration> 

を次のようにクライアントアプリケーションのapp.configをである私は、次のコード

でサービスを呼び出します
ServiceReference1.Service1Client clnt = new ServiceReference1.Service1Client(); 
      clnt.ClientCredentials.UserName.UserName = "peter"; 
      clnt.ClientCredentials.UserName.Password = "grr"; 

      string result=clnt.GetSecuredData(); 

私は間違っていますか?

クライアントアプリケーションとサービスの両方が同じマシン上で実行されていることに注意してください。私は、IDが同じ資格情報であるため、サービスを実行しているマシンまたはクライアントから渡されたものであるかどうかはわかりません.....

他の質問はおそらく "どうしますか私はサービスに渡されたユーザー名とパスワードを取得しますか? "

答えて

5

私は

私は私がまたウェブにいくつかの変更を行うために必要な、ここHow to: Use a Custom User Name and Password Validator

を発見したカスタム検証クラスを作成するために必要な、今これを働いています。コンフィグ

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1Secure.Auth,WcfService1Secure" /> 
      </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="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WsHttpBindingConfig"> 
      <security mode="TransportWithMessageCredential"> 
      <transport clientCredentialType="None" /> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 

    </bindings> 
    <protocolMapping> 
     <add binding="wsHttpBinding" scheme="https" bindingConfiguration="WsHttpBindingConfig" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

とapp.configを

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 

    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IService1" /> 
      </basicHttpBinding> 
      <wsHttpBinding> 
       <binding name="WSHttpBinding_IService1"> 
        <security mode="TransportWithMessageCredential"> 
         <transport clientCredentialType="None" /> 
         <message clientCredentialType="UserName" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="https://localhost/WcfService1Secure/Service1.svc" 
       binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" 
       contract="ServiceReference1.IService1" name="WSHttpBinding_IService1" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

今、ユーザーがリクエストに応じて検証され、ユーザー名が

ServiceSecurityContext.Current.PrimaryIdentityを使用して利用可能です。

+0

これを数回試しましたが、 'ServiceSecurityContext.Current.PrimaryIdentity'にアクセスするときに取得するユーザー名は、認証に使用されたものではないようです。ここに光を当てることはできますか? –

2

こんにちはあなたはサーバー側でログイン名とパスワードを受け入れ、変数やDBに保存することができますメソッドを追加する必要があり
この

​​

または

clnt.SignUp(login,password); 
のようになりますあなたの方法

あなたがやっていることは、スタンドアロンアプリケーションでは動作しますが、Webcallでは動作しません メソッドサービスへの呼び出しは、

のようになります

http://MyService/IMyContract/MyAction1

・ホープ、このヘルプ

+0

私は本当に、各呼び出しで手動で資格情報を渡す必要はありませんでした。確かにService1Clientによって自動的に渡されるべきですか? – coolblue2000

+0

あなたが行っているように、私はあなたがSOAP環境にあることを自動的に受け継がれるのではないかということはすべてが解読されていることを意味します –

+0

私は信任状がプロキシによってソープヘッダーに渡されたと思いましたか?それ以外の場合、資格情報のプロパティは何故ですか?私はこの作業の多くの例を見てきましたが、使用したくない他のものと一緒にsqlmembershipプロバイダを使用しているようです。私はちょうど石鹸のヘッダーによって自動的に渡されたユーザー名とパスワードで認証したいと思う。 – coolblue2000

1

あなたはPerSessionインスタンスを使用しているなら、あなたは、サーバー上の成功に認証を最初に呼び出すことができ、サーバー上のフラグを維持し、後続の呼び出しは、実行する前にフラグをチェックします。つまり、クライアントは最初にAuthenticateメソッドを呼び出してから、他のメソッドだけを呼び出す必要があります。

PerSessionインスタンスを使用しない場合は、サーバー用の独自のプロキシクラスを作成できます。プロキシインスタンスを作成する際に、各呼び出しでヘッダーに渡すことができるLoginName/Passwordを受け取り、サーバー側でカスタムServiceAuthorizationManagerを実装し、 .Current.IncomingMessageHeadersとそれを検証します。

+0

非persessionインスタンスに関しては、デフォルトでscfが行うべきものではないのですか? – coolblue2000