【问题标题】:ServiceStack Session always null in MVC ControllerMVC 控制器中的 ServiceStack 会话始终为空
【发布时间】:2013-01-27 22:04:30
【问题描述】:

我正在使用服务堆栈 MVC 电源包创建一个 ASP.NET MVC 4 应用程序,并利用服务堆栈的身份验证和会话提供程序。我从社交引导 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

    }
}

谁能看到我在这里遗漏了什么?

【问题讨论】:

    标签: asp.net-mvc servicestack


    【解决方案1】:

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

    当从 MVC 中使用 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
    });
    

    【讨论】:

      【解决方案2】:

      尝试将 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
      }
      

      【讨论】:

      • 感谢您的建议,但没有运气。此时会话已填充,我无法使用 ServiceStackController 的 UserSession 属性读取它。但是,我确实发现,如果我使用 ICacheClient Cache 属性,我确实会看到填充的用户会话。那么可能是 SessionAs 扩展方法的问题?将拉下源以查看那里发生了什么
      猜你喜欢
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多