2013-02-12 105 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

相關問題