我有一个运行带有ASP.NET MVC Web应用程序的IIS 7.0的开发服务器,它使用Forms Authentication / Membership进行身份验证.
我需要能够阻止未经授权的用户查看此站点.但是,我们的客户应该能够输入简单的用户名/密码才能获得访问权限.
在他们这样做之后,他们应该能够使用表单身份验证与Web应用程序进行交互,就好像他们刚刚进入一个不受保护的站点一样.
有什么建议?
解决方法
我以前的回答说表格auth和基本的http auth可以在II7集成模式中并存.我完全错了,从那时起就做了一个简单的解决方案.
使用自定义HttpModule,您可以沿着常规格式auth添加基本身份验证
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"); } }
然后在你的web.config中
<system.webServer> <modules> <add name="CustomBasicAuthHttpModule" type="Namespace.CustomBasicAuthHttpModule,AssemblyName"/> </modules> </system.webServer>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。