请稍等,请解释一下这两行代码在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应用程序中进行了添加。但是,即使我没有在请求中传递承载令牌,我仍然能够获取数据。那么添加此过滤器有什么意义呢?
答案 0 :(得分:3)
我通常在代码中保留以下注释,以提醒它们的用途。
// 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