【发布时间】:2012-12-10 21:24:54
【问题描述】:
我已经关注了 ServiceStack 的 SocialBootstrapAPI 中的示例,但我不明白重定向是如何连接的。当我转到未经身份验证的安全控制器时,我被重定向回索引页面。我无法在自己的应用程序中复制该行为。没有找到这些接线的完成位置 - 在 SocialBootstrapApi 的 web.config 中找不到它们?
我继承了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 示例(不完全相同但很接近) - 我不认为这里缺少什么。
那么这是如何工作的呢?
【问题讨论】:
-
您是否为 MVC 控制器设置了 IoC?
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));? -
感谢我在第一次开始修改默认 AppHost 时删除了这一行,现在我重新引入它并且它可以工作。您可以将此作为答案发布。
-
所以答案实际上是两部分,修复会话和使用 CustomAuthenticationMvc 项目来参考如何实现重定向。
-
好的,提升为答案。当然,如果 Mythz 的回答更符合您的要求,您可以接受它。