加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 跨应用程序进行表单身份验证的ServiceStack失败

发布时间:2020-12-16 03:27:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个在api.mydomain.com上运行API的ServiceStack项目.同一解决方案中的管理项目托管在admin.mydomain.com.登录/注销已由管理应用程序处理,但我想确保在我的api调用上对用户进行身份验证(有时也检查权限).我正在使用 forms authentication across project
我有一个在api.mydomain.com上运行API的ServiceStack项目.同一解决方案中的管理项目托管在admin.mydomain.com.登录/注销已由管理应用程序处理,但我想确保在我的api调用上对用户进行身份验证(有时也检查权限).我正在使用 forms authentication across projects,因此我的api项目可以使用auth cookie.

这是api项目中的web.config身份验证标记:

<authentication mode="Forms">
  <forms protection="All" loginUrl="home/denied" slidingExpiration="true" timeout="60" defaultUrl="home/denied" path="/" domain="mydomain.com" name=".myAuth"></forms>
</authentication>

基于此Authentication and authorization post,我在服务方法中添加了[Authenticate]属性,期望它根据IsAuthenticated的值通过/失败.但是,无论是否存在身份验证cookie,它都会每次重定向到“home / denied”. (我通过继承AuthenticateAttribute并检查OriginalRequest来确认这一点…当我使用管理应用程序登录时,cookie集已经存在且req.OriginalRequest.IsAuthenticated为真.)

为什么我的请求被重定向,以及如何正确使用管理应用程序中现有的身份验证凭据?

编辑:这是我提出的解决方案.它只需要一个IPrincipal标识来通过身份验证.

public class AuthenticateAspNetAttribute : RequestFilterAttribute
{
    public override void Execute(IHttpRequest req,IHttpResponse res,object requestDto)
    {
        SessionFeature.AddSessionIdToRequestFilter(req,res,null); //Required to get req.GetSessionId()

        using (var cache = req.GetCacheClient())
        {
            var sessionId = req.GetSessionId();
            var session = sessionId != null ? cache.GetSession(sessionId) : null;
            var originalRequest = (System.Web.HttpRequest) req.OriginalRequest;
            var identity = originalRequest.RequestContext.HttpContext.User.Identity;
            if (!identity.IsAuthenticated)
                AuthProvider.HandleFailedAuth(new BasicAuthProvider(),session,req,res);

        }
    }
}

解决方法

在 Authentication and autorization post你引用它读取:

ServiceStack’s Authentication,Caching and Session providers are
completely new,clean,dependency-free testable APIs that doesn’t rely
on and is devoid of ASP.NET’s existing membership,caching or session
provider models.

这意味着它完全独立,与ASP.NET现有的身份验证提供程序无关.即客户端需要显式调用/ auth服务以使用ServiceStack Web服务进行身份验证.

有关MVC网站的示例,请参阅SocialBootstrapApi示例演示项目,该网站在MVC控制器和ServiceStack Web服务之间使用和共享ServiceStack的身份验证提供程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读