2017-10-08 33 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

相关问题