使用MVC控制器进行ServiceStack身份验证

时间:2012-12-10 21:24:54

标签: asp.net-mvc servicestack

我已经在SocialStootstrapAPi中跟踪了ServiceStack中的示例,但我没有得到重定向的连接方式。当我转到未经身份验证的安全控制器时,我会被重定向回索引页面。我无法在自己的应用程序中复制该行为。没找到这条布线的位置 - 在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示例(不完全相同但接近) - 我认为这里没有遗漏。

那么这是如何运作的?

2 个答案:

答案 0 :(得分:4)

根据OP请求推广对答案的评论:

您为MVC控制器设置了IoC吗? ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));

答案 1 :(得分:2)

请查看此问题,了解ServiceStack身份验证如何使用change the HtmlRedirect

您可能还会发现ServiceStack UseCases中的CustomAuthenticationMvc项目更有用,因为它只关注MVC中的身份验证。