man*_*u08 5 authentication asp.net-mvc iis-7 httpmodule custom-authentication
我有一个ASP.NET MVC应用程序,有一些RESTful服务,我试图使用自定义基本身份验证来保护它们(它们是根据我自己的数据库进行身份验证).我已经通过编写HTTPModule实现了这一点.
我有一个方法附加到HttpApplication.AuthenticateRequest事件,它在身份验证失败的情况下调用此方法:
private static void RejectWith401(HttpApplication app)
{
app.Response.StatusCode = 401;
app.Response.StatusDescription = "Access Denied";
app.CompleteRequest();
}
Run Code Online (Sandbox Code Playgroud)
此方法附加到HttpApplication.EndRequest事件:
public void OnEndRequest(object source, EventArgs eventArgs)
{
var app = (HttpApplication) source;
if (app.Response.StatusCode == 401)
{
string val = String.Format("Basic Realm=\"{0}\"", "MyCustomBasicAuthentication");
app.Response.AppendHeader("WWW-Authenticate", val);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码添加"WWW-Authenticate"标头,告诉浏览器抛出登录对话框.当我使用Visual Studio的Web服务器进行本地调试时,这非常有效.但是当我在IIS7中运行它时失败了.
对于IIS7,除了匿名之外,我都关闭了内置的身份验证模块.它仍然返回HTTP 401响应,但它似乎正在删除WWW-Authenticate标头.
有任何想法吗?
| 归档时间: |
|
| 查看次数: |
1423 次 |
| 最近记录: |