2016-07-10 12 views
-1

私は数日間これを把握しようとしています。私は私が近くにいるが、私が逃しているものを理解できないことを知っている。基本認証とWCF RESTサービスの設定に問題があります

  • サービスは
  • 基本認証がサイトで有効になっているIIS上でホストされているウェブサイトであり、かつ匿名認証など、他のすべてが無効になっています。
  • 私は自分で署名した証明書を作成し、httpsバインディングを設定しました
  • 私はUsernamePasswordValidatorのValidateメソッドをオーバーライドしましたが、ブレークポイントにアタッチするとそれには到達しません。だから、私の問題はおそらくこのことと関係しています。
  • サービスメソッドhttps://localhost/Services/JobSiteService.svc/rest/getにアクセスしようとすると、401の不正なエラーが発生するため、ユーザー名とパスワードの入力を求められます。

誰かが間違っているのを誰も見ることができますか?

以下の適切なコード:

のWeb.Config

<system.serviceModel> 
<bindings> 
    <webHttpBinding> 
    <binding name="webHttpTransportSecurity"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
    </webHttpBinding> 
</bindings> 

<services> 
    <service name="Validity.WebService.JobSiteService" behaviorConfiguration="SecureRestBehavior"> 
    <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="RESTBehavior" bindingConfiguration="webHttpTransportSecurity" contract="Validity.WebService.Contracts.IJobSiteService" /> 
    <endpoint address="meta" binding="mexHttpsBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 

<behaviors> 
    <serviceBehaviors> 
    <behavior name="SecureRestBehavior"> 
     <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 

     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Validity.WebService.IdentityValidator, Validity.WebService" /> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 

    <endpointBehaviors> 
    <behavior name="RESTBehavior" > 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

<protocolMapping> 
    <add binding="webHttpBinding" scheme="https" /> 
</protocolMapping> 

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 

サービス契約:

[ServiceContract] 
public interface IJobSiteService 
{ 
    [OperationContract] 
    [WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json)] 
    List<JobSite> Get(); 
} 

カスタムバリデータ(それは名前空間にあります "Validity.WebService" が、コードフォーマットが壊れました。):

 public override void Validate(string userName, string password) 
    { 
     using (var context = new ValidityContext()) 
     { 

      using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context))) 
      { 
       var user = userManager.Find(userName, password); 
       if (user == null) 
       { 
        var msg = String.Format("Unknown Username {0} or incorrect password {1}", userName, password); 
        throw new FaultException(msg);//the client actually will receive MessageSecurityException. But if I throw MessageSecurityException, the runtime will give FaultException to client without clear message. 
       } 
       else 
       { 

        SessionOperationContext.Current.Items["CurrentUser"] = user; 
       } 
      } 
     } 
    } 

これについてのお手伝いをさせていただきます。

+0

サーバー側の情報のみが表示されています。 IISにAuthorizationヘッダーを提供して最初にフォーカスする必要があるのはクライアントです。 – Crowcoder

+0

Crowcoderありがとうございました。私のカスタム検証メソッドを使用するのではなく、Windowsの認証ヘッダーに対する認証ヘッダーの検証がIISで問題だったようです。 – ErnieStings

答えて

0

最後にこれを考え出しました。

解決策は、IISサーバーで基本認証を無効にし、ServiceAuthorizationManagerのCheckAccessCoreメソッドを無効にしてユーザーを検証することでした。

+0

はい、それは動作しますが、別の質問がwcf webhttpbindingで認証を実装していますか?https://stackoverflow.com/questions/45770217/my-customauthorizationpolicy-evaluate-method-never-fires –

関連する問題