2009-09-03 2 views
3

私は独自のIPrincipal実装をインストールするためにServiceAuthorizationBehaviourに与えることができるカスタムIAuthorisationPolicyを設定しようとしています。私は指示hereに従い、NetNamedPipesバインディングを使って自己ホストするときにこれが動作することを確認するテストを書いた。アイデンティティプロパティがカスタムIAuthorisationPolicyに渡されないのはなぜですか?

問題は、IISでホストされているものを使用しようとすると、Identitiesプロパティが自分のIAuthorisationPolicyに渡されるevaluationContextに設定されていないことです(自己ホスティングの場合と異なります)。

次は私の構成ファイルからの抜粋です:

<customBinding> 
     <binding name="AuthorisedBinaryHttpsBinding" receiveTimeout="00:03:00" sendTimeout="00:03:00"> 
      <security authenticationMode="UserNameOverTransport"> 
      </security> 
      <binaryMessageEncoding> 
      </binaryMessageEncoding> 
      <httpsTransport /> 
     </binding> 
     </customBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="CommonServiceBehaviour"> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="MembershipProvider" 
            membershipProviderName="AdminSqlMembershipProvider"/> 
      </serviceCredentials> 
      <serviceMetadata httpGetEnabled="true" /> 
      <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

(それがここに表示されていない理由である、私はコードをServiceAuthorisationBehaviorを構成してることに注意してください)

任意のアイデア理由私はアイデンティティのプロパティを渡されていないよ?

マイIAuthorisationPolicyは次のようになります。チェックする

public class PrincipalInstallingAuthorisationPolicy : IAuthorizationPolicy 
{ 
    public bool Evaluate(EvaluationContext evaluationContext, ref object state) 
    { 
     var identity = GetClientIdentity(evaluationContext); 
     if (identity == null) 
     { 
      return false; 
     } 
      // var groups = get groups 
      var principal = new GenericPrincipal(identity, groups); 
      evaluationContext.Properties["Principal"] = principal; 


     return true; 
    }  

    private IIdentity GetClientIdentity(EvaluationContext evaluationContext) 
    { 
     object obj; 
     if (!evaluationContext.Properties.TryGetValue("Identities", out obj)) 
     { 
      return null; 
     } 

     IList<IIdentity> identities = obj as IList<IIdentity>; 
     if (identities == null || identities.Count <= 0) 
     { 
      return null; 
     } 

     return identities[0]; 
    } 

    ... 
} 

答えて

1

一つのこと - あなたは本当に(あなたがするリンク先の記事のように)に「カスタム」serviceAuthorizationのprincipalPermissionModeを設定するのですか?それはあなたの成功のために不可欠です。

<serviceAuthorization principalPermissionMode="Custom"> 

また、匿名ユーザーとしてサービスを呼び出している可能性がありますか?その場合、「null」の識別情報が得られる可能性があります。

クライアントの設定はどうですか?どのバインディングを使用しますか、どのセキュリティ設定ですか?

私は同じ問題に取り組んでいたマルク・

+1

Marc私は間違いなくカスタムに設定していますが、匿名ユーザーの頭に釘を打つかもしれないと思います。私はこの動作をすべてのサービスに適用していましたが、一部は匿名です。 –

3

。 HTTPを使用している可能性があります。私の場合は、IISとTCPバインディングで問題ありません。私がbasicHttpBindingに変更したとき、identitiyは送信されるのを止めました。

+0

これは便利な答えです。私は直ちにtcp/ipバインディングへの切り替えを試みましたが、そのバインディングタイプをホストできないIIS Expressを使用していたことを忘れてしまいました。その後、私は必要なセキュリティ機能を備えたWsHttpBindingsの試行を開始しました。 –

関連する問題