2017-10-08 31 views
1

我有.NET MVC5應用程序與ServiceStack。在一個AuthenticationFilter中,我想檢查一個特定的屬性是否在會話中。C#.NET MVC服務棧如何從身份驗證過濾器訪問自定義用戶會話

在AuthController:

var customerSession = SessionAs<CustomerUserSession>(); 
customerSession.property = "some value"; 

在篩選:

public class MyAuthFilter : ActionFilterAttribute, IAuthenticationFilter 
{ 
    public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext) 
    { 
     // I want to access that property here 
    } 
} 

我的自定義會話從服務棧實現AuthUserSession。

在此先感謝!

回答

2

控制器需要繼承ServiceStackController,那麼你應該能夠訪問UserSession:

var ssController = filterContext.Controller as ServiceStackController; 
if (ssController == null) return; 

var session = ssController.ServiceStackProvider.SessionAs<CustomerUserSession>(); 
+0

SessionAs是類ServiceStackController的保護方法,所以我不認爲它可以用於你的方式」已經描述過。 – Nikola

+0

@尼古拉確定你應該可以使用'ServiceStackProvider.SessionAs ();' – mythz

相關問題