我正在使用 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);
}
}
});
最佳答案
尝试向 ADFS 添加“Name Id”声明:
http://darb.io/blog/2014/06/30/WebAPI-and-ADFS-as-external-login-provider/
关于c# - Ws-Federation 身份信息在 Web API 中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40387375/