加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – IIS7 – 密码保护开发服务器

发布时间:2020-12-16 04:25:25 所属栏目:asp.Net 来源:网络整理
导读:我有一个运行带有ASP.NET MVC Web应用程序的IIS 7.0的开发服务器,它使用Forms Authentication / Membership进行身份验证. 我需要能够阻止未经授权的用户查看此站点.但是,我们的客户应该能够输入简单的用户名/密码才能获得访问权限. 在他们这样做之后,他们应
我有一个运行带有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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读