2013-03-06 67 views
2

我有一個使用舊API的webservice。幾乎我所有的服務處理程序都需要身份驗證,因此我使用服務級別的身份驗證屬性。ServiceStack:如何查看當前請求的處理程序是否需要身份驗證

我所有的服務都實現了一個自定義的基本服務,它使用OnBeforeExecute來填充一些與認證有關的屬性。

是否有一種快速的方法來查看當前請求的處理程序是否需要OnBeforeExecute方法中的身份驗證?

我不希望通過使用反射來查看屬性,因爲我知道這是一個相當慢的操作。我也期待在ServiceStack系統已經某處有這個信息在其腹部:)


我最終什麼事做

由於我的項目有,我使用身份驗證的ApplyTo參數服務禁用一些處理程序的身份驗證要求,我結束了以下。

protected override void OnBeforeExecute(TRequestDto request) 
{ 
    base.OnBeforeExecute(request); 

    var attr = FilterAttributeCache.GetRequestFilterAttributes(request.GetType()).OfType<AuthenticateAttribute>().FirstOrDefault(); 
    if (attr != null) 
    { 
     ApplyTo reqmethod = base.Request.HttpMethodAsApplyTo(); 
     if ((attr.ApplyTo & reqmethod) == reqmethod) 
     { 
      // 
      // do stuff that should only be done when 
      // the handler requires authentication 
      // 
     } 
    } 
} 

回答

2

沒有什麼特別之處Authentication請求過濾器屬性,除了它具有lowest priority所以它首先執行。

ServiceStack確實保持所有的FilterAttributeCache屬性請求DTO了,所以你可以使用這個API,以確定是否AuthenticateAttribute針對特定服務定義,如:

var hasAuth = FilterAttributeCache.GetRequestFilterAttributes(request.GetType()) 
    .OfType<AuthenticateAttribute>().FirstOrDefault() != null; 
+0

謝謝Mythz :)我肯定會有一些內置的巧妙方式 – Pingvinen 2013-03-06 16:11:11

相關問題