我有一个运行带有ASP.NET MVC Web应用程序的IIS 7.0的开发服务器,它使用Forms Authentication/Membership进行身份验证.
我需要能够阻止未经授权的用户查看此站点.但是,我们的客户应该能够输入简单的用户名/密码才能获得访问权限.
在他们这样做之后,他们应该能够使用表单身份验证与Web应用程序进行交互,就好像他们刚刚进入一个不受保护的站点一样.
有什么建议?
我以前的回答说表格auth和基本的http auth可以在II7集成模式中并存.我完全错了,从那时起就做了一个简单的解决方案.
使用自定义HttpModule,您可以在常规格式身份验证中添加基本身份验证
public class CustomBasicAuthHttpModule : IHttpModule
{
private HttpApplication httpApplicationContext;
public void Dispose()
{
}
public void Init(HttpApplication context)
{
this.httpApplicationContext = context;
context.BeginRequest += this.OnBeginRequest;
context.EndRequest += this.OnEndRequest;
}
private void OnBeginRequest(object sender, EventArgs e)
{
// your logic of checking Auth header goes here
if (this.httpApplicationContext.Request.Headers["Authorization"] != "Basic base64-encoded-user:pass")
{
this.httpApplicationContext.Response.StatusCode = 401;
this.httpApplicationContext.Response.End();
}
}
private void OnEndRequest(object sender, EventArgs e)
{
if (this.httpApplicationContext.Response.StatusCode == 401)
{
this.httpApplicationContext.Response.AddHeader("WWW-Authenticate", "Basic");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的web.config中
<system.webServer>
<modules>
<add name="CustomBasicAuthHttpModule" type="Namespace.CustomBasicAuthHttpModule, AssemblyName"/>
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Mas*_*sif -1
我们为 IIS 编写了一个自定义模块,以自动允许某些 IP 范围通过,并向其他任何人提供登录对话框。一旦他们登录,它就会将该事实存储在他们的会话中并简单地传递请求。
工作正常,可以应用于 IIS 站点或服务中的任何内容。
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |