2012-01-18 5 views
1

一部のWCFサービスメソッドで承認と監査を行う必要があります。私は自分のコードを汚染しないように属性で処理したいと思います。私はPostSharpを使用してカスタム属性を作成しました。これが適切なアプローチであるか、MSILを調整していることを前提として、PostSharpに認証メカニズムを頼っている問題があるのでしょうか?PostSharpは承認タスクに適していますか?

私のWCFサービスクラスでは、以下のように属性を使用しています。

[AuthoriseAndAudit(UserRoleTypesEnum.Operator)] 
public JSONResult<bool> IsAliveAuthorised() 
{ 
    return new JSONResult<bool>() { Success = true, Result = true }; 
} 

省略された属性コードは以下のとおりです。

using PostSharp.Aspects; 

[Serializable] 
public class AuthoriseAndAuditAttribute : OnMethodBoundaryAspect 
{ 
    private static ILog logger = AppState.logger; 

    private UserRoleTypesEnum _requiredRole = UserRoleTypesEnum.None; 

    public AuthoriseAndAuditAttribute(UserRoleTypesEnum role = UserRoleTypesEnum.None) 
    { 
     _requiredRole = role; 
    } 

    public override void OnEntry(MethodExecutionArgs args) 
    { 
     logger.Debug(String.Format("AuthoriseAndAuditAttribute checking {0}.", args.Method.Name)); 

     // Get the user's session from cookie. 
     UserSession userSession = GetUserSession(); 

     // Check that user is in the required role. 
     bool isAuthorised = (_requiredRole == UserRoleTypesEnum.None || (userSession != null && userSession.Roles.Contains(_requiredRole))); 

     // Write an audit table entry. 
     logger.Debug("Writing audit table entry."); 

     if (!isAuthorised) 
     { 
      logger.Warn("Not authorised for " + args.Method.Name + "."); 
      throw new UnauthorizedAccessException(); 
     } 
    } 
} 

答えて

4

通常、私はイエスと言うだろうが、WCFはPostSharpが冗長で不要な作業になります使用してその承認を行う方法があります。

カスタム認証設定を実装する必要がある場合は、「はい」を選択すると、PostSharpがその設定に大きく役立ちます。

+0

はいWCFにはいくつかの認証メカニズムが組み込まれていますが、SOAPおよび/またはWindowsを中心に構成されています。この場合、私はRESTエンドポイントを公開しています。私のASP.Netメンバーシップ・プロバイダーの小規模なリワークがその仕事をしています。私がPostSharpを使ってやろうとしているのは、私のカスタム認証スキームをよりきれいにすることです。 – sipwiz

+0

その場合、はい。 PostSharpを使用すると、認証/認証スキーマを簡単にクリーンアップして実装できます。 PostSharpをすばやく学ぶ必要がある場合は、http://programmersunlimited.wordpress.com/postsharp-principals/を参照してください。 –

関連する問題