【发布时间】:2015-05-22 13:56:55
【问题描述】:
Webapi 响应状态码 401 时如何禁用 Authentication Required 弹出窗口?
这是我的 webapi 配置
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(Startup.OAuthOptions.AuthenticationType));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
//config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
【问题讨论】:
-
你试过了吗?你能显示一些代码吗?
-
哦,是的,我尝试了一整天都没有成功:(
-
弹出是一种浏览器行为。如果您不想要它,我想您可能必须以自定义方式实现未授权,而不是返回 401,而是返回另一个代码
-
这确实是浏览器行为,特别是由
WWW-Authenticate标头引起的...大多数浏览器在看到该标头时会自动提示基本身份验证,因此关键是弄清楚如何删除该标头...
标签: asp.net asp.net-mvc authentication asp.net-web-api asp.net-web-api2