ServiceStack Session在MVC Controller中始终为null

时间:2013-02-12 14:51:57

标签: asp.net-mvc servicestack

我正在使用服务堆栈MVC powerpack创建一个ASP.NET MVC 4应用程序,利用服务堆栈的auth和会话提供程序。我正在从社交bootstrap API项目中复制许多逻辑。我的控制器继承自以下基本控制器:

public class ControllerBase : ServiceStackController<CustomUserSession>

实现为:

public class ControllerBase : ServiceStackController<CustomUserSession> {}

CustomUserSession继承自AuthUserSession

public class CustomUserSession : AuthUserSession

登录后,我的CustomUserSession OnAuthenticated()方法会运行,但当我重定向回我的控制器时,UserSession没有填充,例如。

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 个答案:

答案 0 :(得分:2)

请参阅ServiceStack Google Group中的问题 - https://groups.google.com/forum/?fromgroups=#!topic/servicestack/5JGjCudURFU

当从MVC中使用JsonSeviceClient(或任何serviceClient)进行身份验证时,cookie不会与MVC请求/响应共享。

在MVC Controller中对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 :(得分: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
}