2012-12-10 57 views
3

我已經遵循了SocialBootstrapAPi中ServiceStack的示例,但我不明白它是如何連接重定向的。當我轉到未經身份驗證的Secured控制器時,我將重定向回索引頁面。我無法在自己的應用程序中複製該行爲。沒有找到這個接線的位置 - 在SocialBootstrapApi的web.config中找不到它們?與MVC控制器的ServiceStack身份驗證

我繼承了ServiceStackController<AuthUserSession>並將[Authenticate]放在我的基礎控制器上。這是我的錯誤:

[NullReferenceException: Object reference not set to an instance of an object.] 
    ServiceStack.ServiceInterface.SessionExtensions.SessionAs(ICacheClient cache, IHttpRequest httpReq, IHttpResponse httpRes) +90 
    ServiceStack.Mvc.ServiceStackController.SessionAs() +64 
    ServiceStack.Mvc.ServiceStackController`1.get_UserSession() +36 
    ServiceStack.Mvc.ServiceStackController`1.get_AuthSession() +5 
    ServiceStack.Mvc.ExecuteServiceStackFiltersAttribute.OnActionExecuting(ActionExecutingContext filterContext) +97 

這是我在AppHost.cs文件:

 //Enable Authentication 
     ConfigureAuth(container); 

     //Register In-Memory Cache provider. 
     //For Distributed Cache Providers Use: PooledRedisClientManager, BasicRedisClientManager or see: https://github.com/ServiceStack/ServiceStack/wiki/Caching 
     container.Register<ICacheClient>(new MemoryCacheClient()); 
     container.Register<ISessionFactory>(c => 
      new SessionFactory(c.Resolve<ICacheClient>())); 

ConfigureAuth()類似於SocialBootstrapApi樣品(不完全一樣,但接近) - 我不認爲這裏有什麼缺失。

那麼這是如何工作的?

+0

您是否爲自己的MVC控制器設置了IoC? 'ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));'? –

+0

謝謝我第一次開始修改默認的AppHost時刪除了這一行,現在我已經重新引入它,它的工作原理。你可以發表這個答案。 – mare

+0

所以答案實際上是兩部分,修復會話並使用CustomAuthenticationMvc項目來參考如何實現重定向。 – mare

回答

4

推進評論一個答案,每個管理員的要求:

你設置你的IoC爲你的MVC控制器? ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));

2

看看這個問題,看看如何使用ServiceStack的認證change the HtmlRedirect

您還可能會發現ServiceStack UseCases中的CustomAuthenticationMvc項目更有用,因爲它只關注MVC中的身份驗證。

+0

感謝參考這個項目,並沒有意識到這一點。 – mare