2013-02-12 75 views
4

我使用服务堆栈MVC powerpack创建一个ASP.NET MVC 4应用程序,利用服务堆栈的身份验证和会话提供程序。我从社交引导API项目复制了很多逻辑。ServiceStack会话在MVC控制器中始终为空

public class ControllerBase : ServiceStackController<CustomUserSession> 

其实现为:

public class ControllerBase : ServiceStackController<CustomUserSession> {} 

CustomUserSession继承AuthUserSession:我控制器从以下基本控制器继承

public class CustomUserSession : AuthUserSession 

我登录后,我CustomUserSessionOnAuthenticated()方法运行,但是当我重新指向我的控制器时,UserSession不是popul例如,

public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
{ 
    base.OnAuthenticated(authService, session, tokens, authInfo); 
    CustomFoo = "SOMETHING CUSTOM"; 

    // More stuff 
} 

public class MyController : ControllerBase 
{ 
    public ActionResult Index() 
    { 
     // After having authenticated 
     var isAuth = base.UserSession.IsAuthenticated; // This is always false 
     var myCustomFoo = base.UserSession.CustomFoo; // This is always null 

    } 
} 

任何人都可以看到我在这里失踪了吗?

回答

2

参考ServiceStack谷歌集团内发出 - 从内部MVC的https://groups.google.com/forum/?fromgroups=#!topic/servicestack/5JGjCudURFU

当与JsonSeviceClient(或任何serviceClient)认证cookie不与MVC请求/响应共享。

当在MVC控制器内对ServiceStack进行身份验证时,应将MVC HttpContext发送到ServiceStack。像下面的东西应该工作...

var authService = AppHostBase.Resolve<AuthService>(); 
authService.RequestContext = System.Web.HttpContext.Current.ToRequestContext(); 
var response = authService.Authenticate(new Auth 
{ 
    UserName = model.UserName, 
    Password = model.Password, 
    RememberMe = model.RememberMe 
}); 
1

尝试设置IsAuthenticated为true,并节省您的会话......

public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
{ 
    base.OnAuthenticated(authService, session, tokens, authInfo); 
    CustomFoo = "SOMETHING CUSTOM"; 
    session.IsAuthenticated = true; 
    authService.SaveSession(session); 

    // More stuff 
} 
+0

感谢您的建议,但没有运气。此时会话已填充,我无法使用ServiceStackController的UserSession属性读取它。然而,我发现如果我使用ICacheClient Cache属性,我会看到填充的用户会话。所以可能是SessionAs扩展方法的问题?要拉下来源看看那里发生了什么 – pjacko 2013-02-14 03:20:41