2012-03-22 18 views
1

私は認証を機能させようとしているテストWCFアプリケーションを作成しましたが、単にメソッドを実行してログイン/認証を要求しません。私がすることが出来るのですWCF認証で資格情報が要求されない

public class Authorization : UserNamePasswordValidator 
    { 
     public override void Validate(string userName, string password) 
     { 
      if (null == userName || null == password) 
      { 
       throw new ArgumentNullException(); 
      } 

      if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) 
      { 
       // This throws an informative fault to the client. 
       throw new FaultException("Unknown Username or Incorrect Password"); 
       // When you do not want to throw an infomative fault to the client, 
       // throw the following exception. 
       // throw new SecurityTokenException("Unknown Username or Incorrect Password"); 
      } 
     } 
    } 

マイService.svc.csクラス

public string Hello(string message) 
{ 
    return "You typed: " + message; 
} 

<bindings> 
     <wsHttpBinding> 
      <binding name="Binding1"> 
       <security mode="Message"> 
        <message clientCredentialType="UserName" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
</bindings> 

<serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyAPI.Authorization, App_Code" /> 
</serviceCredentials> 

マイ承認クラス:以下は私のWCFのアプリで私のweb.configファイル内のコードスニペットです認証を必要とするか、クラスの上にこのメソッドの上にいくつかの属性を入れてください。

私は、その後のテストコンソールアプリケーションを作成して、ここにコードがある:認証を求めず:

public static Test.Service1Client client = new Test.Service1Client(); 
     static void Main(string[] args) 
     { 
      Console.WriteLine(client.Hello("hello")); 
      Console.ReadLine(); 
     } 

これだけで「こんにちはあなたが入力した」を出力します。ここに私はapp.configの抜粋です:

<system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
        useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <security mode="None"> 
         <transport clientCredentialType="None" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="UserName" algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://MyServer/Service1.svc" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_IService1" contract="Test.IService1" 
       name="BasicHttpBinding_IService1" /> 
     </client> 
    </system.serviceModel> 

私がやっによってclient.Hello("hello")を呼び出す前に、ログイン資格情報を設定する必要が期待される:

client.ClientCredentials.UserName.UserName = "test1"; 
client.ClientCredentials.UserName.Password = "1tset"; 

しかし、明らかではない

を編集

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

    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
     <bindings> 
      <wsHttpBinding> 
       <binding name="Binding1"> 
        <security mode="Message"> 
         <message clientCredentialType="UserName" /> 
        </security> 
       </binding> 
      </wsHttpBinding> 
     </bindings> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior> 
        <serviceCredentials> 
    <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyAPI.Authorization, App_Code" /> 
</serviceCredentials> 
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
        <serviceMetadata httpGetEnabled="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> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 
+0

通常、ブラウザなどのクライアントは、資格情報の入力を要求します。あなたのテストコンソールアプリケーションは、そのようにコード化していないので、プロンプトを表示しません。資格情報を提供していない場合、そのサービスコールは失敗しているはずです。サーバ設定のBinding1バインディングでサービスをバインドしているかどうか確認してください。 – VinayC

+0

@VinayC - それはどこで確認できますか?私の編集を参照してください、それは私のサービスから私のweb.configファイル全体を含んでいます – CallumVass

答えて

0

間違ったバインディングを使用しているようですが、クライアントはBasicHttpを使用していますサーバー上でWsHttpBindingを定義している間にバインドします。

0

WCF/IISは、構成を必要とせずに、あなたが持っているサービスを魔法のように接続します(この機能の名前は忘れてはいけません...)。しかし、いくつかのカスタムバインディング設定を定義しています。これは問題ありませんが、これを使用するようにサービスに指示する必要があります。

あなたが好きなもの、あなたのサーバの設定に<service>要素を追加する必要があります。

<system.serviceModel> 
    ... 
    <services> 
     <service name="FullClassNameOfYourService"> 
     <endpoint binding="wsHttpBinding" 
        bindingConfiguration="Binding1" 
        contract="FullClassNameOfYourServiceContract" /> 
     </service> 
    </services> 

はまた、あなたのクライアントの設定はwsHttpBinding要素が含まれていないという事実は、HTTPSは、ウェブサイトのために有効になっていないことを示唆していますあなたのサービスをホストしています。

+0

私は今得ます:契約名 'MyAPI.IService1'は 'Service1'サービスによって実装された契約のリストに見つかりませんでした。 – CallumVass

+0

'name'属性の値は単に" Serivce1 "ですか?あなたの契約タイプに基づいて、サービス名は "MyAPI.Service1"でなければならないと思います。クラス名には名前空間も必要です。 –

+0

私はこのセットがあります: '<サービス名= "MyAPI.Service1"> <エンドポイントバインディング= "wsHttpBinding" bindingConfiguration = "Binding1" 契約= "MyAPI.IService1"/>' – CallumVass

0

あなたのuserNameAuthenticationが正しくありません。 customUserNamePasswordValidatorTypeは、「[完全修飾アセンブリ+クラス名]、[名前空間]」の形式である必要があります。私はあなたの名前空間があなたのポストからあるものを言うことができない、しかし、のようなもの:他人として

<userNameAuthentication userNamePasswordValidationMode="Custom" 
customUserNamePasswordValidatorType="MyNamespace.Authorization , MyNamespace" /> 

は、あなたのクライアントがサービスに接続するために同じ結合型を使用しなければならない、と述べています。

また、サーバー側では、セキュリティモードをnoneに設定しましたが、トランスポートとメッセージタグがあります。セキュリティタグ内。セキュリティとしてNoneを実行している場合、トランスポートとメッセージのセキュリティ仕様は無視されます。つまり、セキュリティモードが[なし]の場合、クライアントの資格情報タイプは無視され、クライアントは認証する必要がありません。

+0

私はそれを試みましたが、それはエラーを投げつけ続けました。なぜ私はAuthorizationクラスをApp_Codeの中に置き、それを使用したのですか?私は今、トランスポートにセキュリティタイプを入れ、クライアントとサーバの両方で同じバインディングタイプを設定しています(どちらもbasicHttpbindingを使用しています)が、次のエラーが表示されます: '提供されたURIスキーム 'http'は無効です。予想される「https」。 パラメータ名:via – CallumVass

+0

「エラー」は私にはあまり役に立ちません。別のエラーが発生した場合、App_CodeをApp_Codeに入れることはどうしたら分かりますか? httpsを探している場合は、トランスポートレベルのセキュリティが指定されているようです。 –

+0

「エラー」とは、App_Codeを使用するように変更するまではエラーを投げていたことを意味しています。 – CallumVass

0

簡易設定ファイルを持つWCF 4を使用します。それはメリットがありますが、デバッグするのは難しいです。あなたのカスタムwshttpbindingが適用されていないと思われます。 (WCF 3.5のような)より冗長configutationをお試しください:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="NewBinding0"> 
      <security mode="TransportWithMessageCredential"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service behaviorConfiguration="WcfService6.Service1Behavior" 
     name="WcfService6.Service1"> 
     <endpoint address="" binding="wsHttpBinding" bindingConfiguration="NewBinding0" 
      contract="WcfService6.IService1"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WcfService6.Service1Behavior"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="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> 
    </system.serviceModel> 
関連する問題