c# - Ws-Federation 身份信息在 Web API 中不可用?

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

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

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

以下是我在 OWIN Startup 类中配置身份验证中间件的方式:

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# - Ws-Federation 身份信息在 Web API 中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40387375/

相关文章:

c# - 流行网站帖子的流行度衰减算法

c# - 无线事件是内存泄漏吗?

c# - 在linq to sql中填充对象列表

html - 在运行时对 css 差异感到困惑

c# - IELaunchURL() 返回 HRESULT 80070012 ('There are no more files.' )

c# - 在不可变对象(immutable对象)和值对象的结构之间进行选择

asp.net - 配置 IIS 以进行 SAML 身份验证

asp.net - XML 序列化迁移到 MySql

c# - EntityState.Modified 是否会导致执行插入操作

asp.net-mvc - 如何在 MVC 运行时自定义 Display 和 Required 属性