c# - HostAuthenticationFilter 有什么作用?

标签 c# asp.net-web-api owin

有人能解释一下这两行代码在我的 WebApiConfig.cs 文件的 Register() 方法中的含义吗。

// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

我假设它在应用程序范围内添加 HostAuthentication。但即使我没有通过我的请求传递不记名 token ,我仍然能够获取数据。那么添加这个过滤器有什么意义呢?

最佳答案

我通常会在我的代码中保留以下注释,以提醒它们的用途。

// Configure Web API to use only bearer token authentication.
// If you don't want the OWIN authentication to flow to your Web API then call 
// SuppressDefaultHostAuthentication on your HttpConfiguration. 
// This blocks all host level authentication at that point in the pipeline.
config.SuppressDefaultHostAuthentication();
//config.Filters.Add(new HostAuthenticationFilter(Microsoft.Owin.Security.OAuth.OAuthDefaults.AuthenticationType));

// “Host-level authentication” is authentication performed by the host (such as IIS), 
// before the request reaches the Web API framework. 
// ----
// Often, you may want to to enable host-level authentication for the rest of your application, 
// but disable it for your Web API controllers. For example, a typical scenario is to 
// enable Forms Authentication at the host level, but use token-based authentication for Web API.
// ----
// To disable host-level authentication inside the Web API pipeline, call config.SuppressHostPrincipal() 
// in your configuration. This causes Web API to remove the IPrincipal from any request that enters 
// the Web API pipeline. Effectively, it "un-authenticates" the request.
config.SuppressHostPrincipal();

此外,如果您仍然可以访问操作数据,则可能是您没有将 [Authorize] 属性应用于 Controller 或操作以限制访问。

相关阅读Host authentication and Web API with OWIN and active vs. passive authentication middleware

关于c# - HostAuthenticationFilter 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50992615/

相关文章:

c# - 拒绝 token 请求时自定义 OWIN/OAuth HTTP 状态代码

asp.net-mvc - 使用 IIS 基本身份验证的 OWIN 身份验证

c# - Linq to NHibernate 返回与 HQL 不同的结果?

c# - 动态构建 Azure DocumentDB 查询

c# - 没有管理用户配置文件权限的 Sharepoint UserProfileManager

c# - 如果我有对象 ID,如何使用 REST API 获取 AzureAD 中对象的显示名称?

asp.net-web-api - ASP.NET Core 中的 IHttpActionResult 和辅助方法

c# - 如何从 Azure 函数中获取多个 blob?

asp.net-core - Swagger Swashbuckle Asp.NET Core : show details about every enum is used

c# - 健康检查 MassTransit 和 Rabbitmq