【问题标题】:Ws-Federation Identity Information Not Available in Web API?Web API 中不提供 Ws-Federation 身份信息?
【发布时间】:2017-03-16 04:53:40
【问题描述】:

我正在使用 Ws-Fed 身份验证 OWIN 中间件来使用 ADFS 对带有 Web API 端点的 ASP.NET MVC 应用程序进行身份验证。我可以使用 ADFS 成功登录,并且在我的 MVC 控制器上,HttpContext.User.Identity.IsAuthenticated 为真 - 我也可以看到已登录用户的声明信息。

但是,对于 WebAPI 端点,User.Identity.IsAuthenticated 为 false。已签名用户的声明信息也不可用。有什么方法可以公开用户已针对 MVC 和 WebAPI 控制器进行身份验证的事实?

这是我在 OWIN 启动类中配置身份验证中间件的方式:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            ExpireTimeSpan = TimeSpan.FromMinutes(sessionDuration), 
            SlidingExpiration = true //expiration extended after each request
        });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = metadata,
                Notifications = new WsFederationAuthenticationNotifications
                {
                    AuthenticationFailed = context =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("/?loginfailed=loginfailed");
                        return Task.FromResult(0);
                    }
                }
            });

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    【讨论】:

    • 我不怕。我什至尝试将姓名 ID 作为唯一声明发送;即使这样也没有用——MVC 控制器说 isAuthenticated = true,Web API 说 false :(
    • 我在本地对此进行了测试,它对我有用。如果可能,请从您的一个 Web Api 控制器中获取以下三个值,并告诉我您得到了什么:Thread.CurrentPrincipal.Identity.IsAuthenticated、HttpContext.Current.User.Identity.IsAuthenticated、User.Identity.IsAuthenticated。我在某处读到需要为 Web Api 设置前两个。
    • 也看看这个。里面可能有一些东西可以帮助你弄清楚:wiki.servicenow.com/…
    • 感谢您的链接和建议。 Thread.CurrentPrincipal.IdentityIsAuthenticated、HttpContext.Current.User.Identity.IsAuthenticated 和 User.Identity.IsAuthenticated 都是假的。它对你有用的事实给了我希望。我可以查看您的 startup.cs 代码作为示例吗?我一定是错过了什么......
    • 我使用的是 Asp.Net Identity,所以设置不同,不幸的是它对您没有帮助。我询问所有这些值的原因是这些值应该由您的身份验证方法设置以使 Web Api 正常工作,因此请检查您的代码并确保您没有覆盖它并且 OWIN 正确设置它。尝试获取 OWIN 上下文,看看是否有用户:stackoverflow.com/questions/31160965/…
    【解决方案2】:

    所以我通过检查CodePlex 上的 OWIN cookie 身份验证中间件源代码找到了这个问题的答案。由 MVC 控制器使用中间件创建的 cookie 的创建方式与创建 Web API 的 cookie 不同。 MVC cookie 是对存储在 session 中的用户信息的引用,由于 Web API 是完全无状态的(无 session),因此在 MVC 中创建的 cookie 不能在 Web API 中使用。

    此外,无论如何在 Web API 中使用 cookie 身份验证都是不好的做法;不记名令牌身份验证是一个更好的选择。

    在我需要使用 Ws-Federation 身份验证的情况下,解决方案是:

    1. 将不记名令牌身份验证中间件添加到我的应用程序

    2. 1234563生成的不记名令牌
    3. 在 MVC 中进行身份验证后,序列化声明,并使用之前创建的端点将它们编组到 Web API

    4. 将不记名令牌添加到 SPA 中的隐藏字段

    非常感谢 @Juan 为我提供反馈和链接,为我指明正确的方向。

    【讨论】:

    • 您能否提供一些示例代码,说明如何在 ADFS 登录页面后获取声明?或者指点我一些文章/帖子?
    猜你喜欢
    • 2014-11-27
    • 2017-01-29
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2018-03-05
    相关资源
    最近更新 更多