c# - HostAuthenticationFilter 有什么作用?
问题描述
谁能解释一下我的 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。但即使我的请求没有传递不记名令牌,我仍然能够获取数据。那么添加这个过滤器有什么意义呢?
解决方案
我通常在我的代码中保留以下注释,以提醒它们的用途。
// 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]
属性应用于控制器或操作以限制访问。
相关阅读Host authentication and Web API with OWIN and active vs. passive authentication middleware
推荐阅读
- python - 在 Python 中,按照下面提到的代码写入文本文件时出现空白行的原因是什么?
- oracle - DB Link 抛出 ORA-28511:丢失 RPC 连接到使用 SID 的异构远程代理
- amazon-web-services - 如何获取 Terraform 计数的值
- javascript - 如何以角度形式修复“无法读取未定义的属性'选项'”?
- python - python IndexError:jupyter中标量变量的索引无效
- mysql - mySQL 选择另一个表中不存在的每条记录
- angular - 正在更新根组件角度中使用的所有相同的多个组件中的变量
- javascript - 无法获得条带支付意图成功元数据
- github - 在 Google Cloud Build yaml 中克隆私有 GitHub 存储库
- python - 如何将列表中的项目分配给变量