4

我正在使用服务堆栈 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

    }
}

谁能看到我在这里缺少的东西?

4

2 回答 2

2

请参阅 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
});
于 2013-02-14T22:33:03.337 回答
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
}
于 2013-02-13T05:41:04.467 回答