カスタムの USERNAME-PASSWORD バリデーターを使用して、WCF サービスを開発しています。
UserNamePasswordValidatorから継承するCustomUserNameValidatorがあります。
IAuthorizationPolicyから継承するCustomAuthorizationPolicyも使用します。
カスタム承認ポリシーのEvaluateメソッドは次のようになります。
// this method gets called after the authentication stage
public bool Evaluate(EvaluationContext evaluationContext, ref object state)
{
// get the authenticated client identity
IIdentity client = GetClientIdentity(evaluationContext);
// set the custom principal
evaluationContext.Properties["Principal"] = new CustomPrincipal(client);
return true;
}
ご覧のとおり、 Evaluateを呼び出すたびに新しいCustomPrincipalオブジェクトを作成します(これは、サービスで呼び出されるすべての操作で行われます)。
これは私のCustomPrincipalコンストラクターがどのように見えるかです:
public CustomPrincipal(IIdentity identity)
{
IdentityObject = identity;
GetUser(IdentityObject.Name);
EnsureRoles();
}
GetUserメソッドとEnsureRolesメソッドは、SQL データベースにアクセスして、ユーザーのロールを確認します。
私の質問は-なぜこれがすべての操作で発生する必要があるのですか??
クライアントがサービスに初めて接続するときだけでなく、すべての操作でCustomPrincipalの作成が行われるのはなぜですか?
サービスで呼び出されるすべての操作について、 「CustomPrincipal」がデータベースにアクセスし、ユーザーとそのすべてのロールを再取得する理由がわかりません...
[更新] これは、クエリ サービス インターフェイスの外観です。
[ServiceContract(Namespace = "http://www.my1fj.com/QueryService/2012/", SessionMode = SessionMode.Required, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
public interface IQueryService
{
// Some operations
}