【问题标题】:Using ServiceStack's AuthService with Controller and AuthorizeAttribute将 ServiceStack 的 AuthService 与 Controller 和 AuthorizeAttribute 一起使用
【发布时间】:2012-06-28 09:37:08
【问题描述】:

没有像经典 ASP.NET MVC 那样简单的 ServiceStack 示例。 AppHarbor 和 SocialBootStrap api 都基于 SinglePage 应用程序。如何使用 Servicestack 的身份验证实现经典的 Asp.net MVC 应用程序。

我已将 AuthService 属性添加到 BaseController (Autowired)。然后我在实施时遇到了奇怪的“对象引用”异常。 (AuthService 第 135 行 base.RequestContext 为空)

为什么我会收到此错误? 或者任何人都可以发送简单的用法。 (如 Asp.net Mvc Membership Starter)

【问题讨论】:

    标签: asp.net-mvc authentication servicestack


    【解决方案1】:

    使用RequestContext的服务,需要注入当前的RequestContext,例如:

    authService.RequestContext = new HttpRequestContext(
        HttpContext.Current.Request.ToRequest(),
        HttpContext.Current.Response.ToResponse(),
        null
    );
    

    这使用SessionExtensions.cs 中的扩展方法。

    您还需要解决来自 IOC 的任何服务 - 这是 sharing functionality between ServiceStack and MVC 的一个很好的示例。

    【讨论】:

    • @ozz 我很好奇,你如何使用 BaseController 的 AuthService 来授权访问操作方法?我正在尝试使用自定义 CredentialsAuthProvider 但没有成功。
    • 发现我的代码有问题:我没有设置 session.IsAuthenticated = true,一旦认证成功。我可以确认 [Authenticate] 现在可以正常工作了。
    • 此代码现在不起作用。没有 ToRequest() 或 ToResponse 扩展。
    • @ozz 不起作用与您无法找到它不同。使用 ReSharper 或 Ctrl+Shift+F 搜索 ToRequest(this 或让其他人为您进行搜索。无论哪种方式,查找参考都是一个已解决的问题,有很多解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多