2015-05-11 24 views
5

我有一個自我託管的服務堆棧應用程序,其中我使用Facebook Oath和Redis - Facebook和redis方面的東西似乎正在工作即。當我訪問AuthUserSession在成功驗證後在ServiceStack服務中爲null

abc.com/auth/facebook 

自定義用戶會話獲取在OnAuthenticated方法中填充。 Redis緩存中的數據正確保存..非常好

問題我的理解是如何在後續請求中檢索此CustomUserSession。要使用OAuth的重定向頁面「/關於-US」開始是我想要檢索會話值但它始終是空

[DataContract] 
public class CustomUserSession : AuthUserSession 
{  
    [DataMember] 
    public string CustomId { get; set; } 

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo) 
    { 
     // receiving session id here and can retrieve from redis cache 
    } 
    public override bool IsAuthorized(string provider) 
    { 
     // when using the [Authenticate] attribute - this Id is always 
     // a fresh value and so doesn't exist in cache and cannot be auth'd 
     string sessionKey = SessionFeature.GetSessionKey(this.Id); 

     cacheClient = ServiceStackHost.Instance.TryResolve<ICacheClient>(); 
     CustomUserSession session = cacheClient.Get<CustomUserSession>(sessionKey); 

     if (session == null) 
     { 
      return false; 
     } 
     return session.IsAuthenticated; 
    } 
} 


[DefaultView("AboutUs")] 
public class AboutUsService : AppServiceBase 
{ 
    public object Get(AboutUsRequest request) 
    { 
     var sess = base.UserSession; 
     return new AboutUsResponse 
     { 
      //custom Id is always null?? 
      Name = sess.CustomId 
     }; 
    } 
} 
public abstract class AppServiceBase : Service 
{ 
    protected CustomUserSession UserSession 
    { 
     get 
     { 
      return base.SessionAs<CustomUserSession>(); 
     } 
    } 
} 

我如何註冊緩存&會議等

 AppConfig = new AppConfig(appSettings); 
     container.Register(AppConfig); 
     container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("10.1.1.10:6379")); 
     container.Register(c => 
       c.Resolve<IRedisClientsManager>().GetCacheClient()); 
     ConfigureAuth(container, appSettings); 

ConfigureAuth的)中的內容(

 var authFeature = new AuthFeature(
      () => new CustomUserSession(), 
      new IAuthProvider[] 
      { 
       new FacebookAuthProvider(appSettings), // override of BasicAuthProvider 
      } 
      ) {HtmlRedirect = null, IncludeAssignRoleServices = false}; 

     Plugins.Add(authFeature); 

我覺得我缺少明顯的東西在這裏....在此先感謝

回答

0

註冊即可使用您鍵入的會話一個CustomUserSession,它需要被指定當您註冊AuthFeature,e.g:

Plugins.Add(new AuthFeature(() => new CustomUserSession(), ...)); 
+0

@MikeW難道只是'CustomId'這是空?如果是的話,你在哪裏填充它,並保存後的會話? – mythz

+0

會話對象是new(),當OnAuthenticate被調用像CustomId和Facebook Email等時,會話的所有值都在那裏。在Redis監視器中,我可以看到會話數據使用密鑰進行SET設置。然後重定向到我嘗試檢索值的剃刀頁面 - 如果有幫助,我可以將項目代碼鏈接到您 – MikeW

+0

@MikeW在Redis Monitor中的「SET」是否包含「CustomId」?您是否也可以看到'GET' redis請求訪問自定義會話,是否使用相同的會話密鑰? – mythz

相關問題