5

したがって、基本的には、wsHttpBindings と、HTTPS を介したカスタム認証を使用する WCF サービスを使用して、すべてを稼働させています。

私が抱えている問題は、customUserNamePasswordValidatorType にあります。

  <serviceCredentials>
    <!-- Use our own custom validation -->
    <userNameAuthentication userNamePasswordValidationMode="Custom"
                            customUserNamePasswordValidatorType="CustomValidator.CustomUserNameValidator, CustomValidator"/>
  </serviceCredentials>

ここにある指示に従って、カスタムクラスも作成しました。

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


            if (!AuthenticateUser(userName, password))
                throw new SecurityTokenValidationException("Invalid Credentials");

エラーは「ファイルまたはアセンブリ 'CustomValidator' またはその依存関係の 1 つを読み込めませんでした。指定されたファイルが見つかりません。」であり、customUserNamePasswordValidatorType の末尾 - 「..., CustomValidator」を参照しています。

カスタムバリデーターを独自の名前空間とクラスに持つことは問題ではないと思いましたが、これを機能させるために他に何をすべきかわかりません。

最初に名前空間の有無にかかわらず、スワッピングなどを試しましたが、何もしませんでした。

別の目がこれを見つけられることを願っています。

ありがとう。

system.serviceModel を編集

  <system.serviceModel>
    <bindings>

      <!-- wsHttpBinding -->
      <wsHttpBinding>
        <binding name="wsHttpEndpointBinding">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>

      <!-- webHttpBinding -->
      <webHttpBinding>
        <binding name="wsHttps" >
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>

      <!-- Basic binding -->
      <basicHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <message clientCredentialType="UserName"/>
            <!-- transport clientCredentialType="None"/-->
          </security>
        </binding>
      </basicHttpBinding>

      <!-- customBinding>
        <binding name="WebHttpBinding_IService">
          textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
              messageVersion="Soap12" writeEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </textMessageEncoding>
          <httpsTransport manualAddressing="false"/>
        </binding>
      </customBinding -->
      <!-- Another custom binding -->
      <customBinding>
        <binding name="CustomMapper">
          <webMessageEncoding  webContentTypeMapperType=
                 "IndexingService.CustomContentTypeMapper, IndexingService" />
          <httpTransport manualAddressing="true" />
        </binding>
      </customBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">




        <!-- Service Endpoints -->
        <!-- since we're hosting in IIS, baseAddress is not required 
        <host>
          <baseAddresses>
            <add baseAddress="https://mysslserver.com/Service.svc"/>
          </baseAddresses>
        </host>
        -->
        <endpoint address="https://mysslserver.com/Service.svc"
                  binding="wsHttpBinding"
                  bindingConfiguration="wsHttpEndpointBinding" 
                  contract="IService"
                  name="wsHttpEndpoint">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <!--identity>
            <dns value="https://mysslserver.com"/>
          </identity-->
        </endpoint>

        <!-- endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/ -->
      </service>
    </services>
    <behaviors>

      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- Setup Security/Error Auditing -->
          <serviceSecurityAudit auditLogLocation="Application"
                suppressAuditFailure="false"
                serviceAuthorizationAuditLevel="Failure"
                messageAuthenticationAuditLevel="Failure" />

          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"
                           httpsGetUrl="https://mysslserver.com/Service.svc"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <!-- Use our own custom validation -->
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="CustomValidator.CustomUserNameValidator, CustomValidator"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>

      <!-- serviceBehaviors>
        <behavior name="ServiceBehavior">
        <serviceMetadata httpsGetEnabled="true" 
                           httpsGetUrl="https://mysslserver.com/Service.svc" />
          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-->
    </behaviors>
  </system.serviceModel>
4

4 に答える 4

15

私はそれに別の刺し傷を与えることに決めました、そして私のカスタムバリデーターを別のライブラリに置くのは好きではありませんでした。

そこで、App_Codeで新しいクラスを作成し、それを実行しました...

以下は実際にそれを修正したものです、

="CustomValidator.CustomUserNameValidator, App_Code"
于 2009-12-12T00:46:14.857 に答える
10

値を使用してカスタムバリデーターを参照する場合

="CustomValidator.CustomUserNameValidator, CustomValidator"

最初の値はタイプ名で、2番目の値はタイプを検索するアセンブリの名前です。したがって、最初のインスタンスでは、サービスは実際にはMyServiceなどの他のアセンブリにあることをお勧めします。その場合、設定ファイルで次のように言う必要があります。

="CustomValidator.CustomUserNameValidator, MyService"

バリデーター用の新しいクラスライブラリを作成したときに、プロジェクトCustomValidator(CustomValidator.dllというアセンブリを出力します)を呼び出したので、構成が機能するようになりました(つまり、別のクラスライブラリ-web.configのアセンブリ参照の名前が有効になっただけです)

于 2009-11-27T02:48:43.717 に答える
2

少し奇妙に思えますが、解決策は別のクラス ライブラリを作成し、WCF サービスでその DLL を参照することでした。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.ServiceModel;

/// <summary>
/// Summary description for CustomUsernamePasswordValidator
/// </summary>
namespace CustomValidator
{
    public class CustomUserNameValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }


            if (!AuthenticateUser(userName, password))
                throw new SecurityTokenValidationException("Invalid Credentials");
            else
            {
                // do nothing - they're good
            }
        }

        public bool AuthenticateUser(string userName, string password)
        {
            if (userName != "userbill" || password != "passwordbill")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}

次に、System.IdentityModel と System.ServiceModel への参照を追加しました。

WCF サービスの serviceCredentials セクションが次のように変更されました。

<serviceCredentials>
    <!-- Use our own custom validation -->
    <userNameAuthentication userNamePasswordValidationMode="Custom"
                            customUserNamePasswordValidatorType="CustomValidator.CustomUserNameValidator, CustomValidator"/>
</serviceCredentials>

それが誰かを助けることを願っています。

無効な資格情報でこれを試しましたが、「資格情報が無効です」というメッセージが表示されることを期待していました。代わりに、「メッセージ内の少なくとも 1 つのセキュリティ トークンを検証できませんでした」というメッセージが表示されます。

それ以外は、これがついに稼働しています!

于 2009-11-26T22:28:32.587 に答える