简体   繁体   English

IIS7基本身份验证,用于保护使用表单身份验证的站点

[英]IIS7 basic authentication to protect a site that uses forms authentication

This should be much simpler than it has proven to be! 这应该比它证明的要简单得多!

I have an ASP.Net web app which uses FORMS authentication to secure part of the site (ie the member login area). 我有一个ASP.Net Web应用程序,它使用FORMS身份验证来保护站点的一部分(即成员登录区域)。

Now I simply want to put a traditional/simple browser password popup (directory security) across the whole site because we are testing it and don't want anyone to stumble across the site and see the unfinished version etc! 现在我只想在整个网站上放置一个传统/简单的浏览器密码弹出窗口(目录安全性),因为我们正在测试它,并且不希望任何人偶然发现整个网站并看到未完成的版本等!

This used to be super simple in older IIS versions. 在旧的IIS版本中,这曾经非常简单。

I have "installed basic authentication" (as IIS7 doesn't come out of the box with this now). 我已经“安装了基本身份验证”(因为IIS7现在没有开箱即用)。 But when I enable it, it tells me that I can't have that enabled at the same time as any redirect based authentication (which is what my FORMS authentication uses). 但是当我启用它时,它告诉我我不能同时启用任何基于重定向的身份验证(这是我的FORMS身份验证使用的)。

So that's just stupid. 所以这只是愚蠢的。

There has to be a super simple way to simply put a cheap popup password across the whole site without impacting on the other authentication method that you have setup inside web.config for the actual application. 必须有一种超级简单的方法来简单地在整个站点上放置一个廉价的弹出密码,而不会影响您在web.config中为实际应用程序设置的其他身份验证方法。

Many thanks.. 非常感谢..

UPDATES IP access restrictions are no good for a couple of reasons: - My IP is dynamic and therefore constantly changing. 更新 IP访问限制并不是出于以下几个原因: - 我的IP是动态的,因此不断变化。 - I don't want to bother anyone that needs to see the site by asking them to bring up a console on their machine and work out their IP address or check their router etc. Many of them are non-technical business users and it will take them an hour to work out their IP address. - 我不想打扰任何需要看到该网站的人,要求他们在他们的机器上调出一个控制台并计算他们的IP地址或检查他们的路由器等。其中许多是非技术性的商业用户,它会花一个小时来计算他们的IP地址。 - Both basic auth and windows auth don't allow the underlying forms authentication to remain in place underneath. - 基本身份验证和Windows身份验证都不允许底层表单身份验证保留在下面。

What we seem to have here is a massive case of Microsoft trying to over-engineer things and as a result a super simple age-old requirements is no longer possible or easily achieveable. 我们在这里看到的是一个巨大的案例,微软试图过度设计事物,因此超级简单的古老要求不再可能或不易实现。 This has to be possible somehow... ANYONE??? 这有可能以某种方式...任何人???

I am running into the same issue. 我遇到了同样的问题。 I will be putting up a beta site limited to a preview group. 我将建立一个仅限于预览组的测试版网站。 The website uses forms authentication but some people from the preview group will have website accounts and others will not. 该网站使用表单身份验证,但预览组中的某些人将拥有网站帐户,而其他人则不会。 Irrespective everyone will need to authenticate at root to gain access to the preview. 无论每个人都需要在root身份验证才能访问预览。

So far the only thing I have working exactly the way I want it is Helicon Ape . 到目前为止,我唯一能按照我想要的方式工作的是Helicon Ape I am running the trial and so far so good. 我正在进行试验,到目前为止一直很好。

Standard .htaccess file in root. root中的标准.htaccess文件。

AuthUserFile c:\fakepath\.htpasswd
AuthType Basic
AuthName "SITE SECURITY"
Require valid-user

User .htpasswd to add a username and password: username:encryptedpassword . 用户.htpasswd添加用户名和密码: username:encryptedpassword

What you describe is a limitation of default ASP.NET, it is only built to run one authentication module at a time. 您所描述的是默认ASP.NET的限制,它仅构建为一次运行一个身份验证模块。 The solution is to build your own IHttpModule that does your custom authentication, and if successful, pass the request to ASP.NET which continue to use Forms. 解决方案是构建自己的IHttpModule来执行自定义身份验证,如果成功,则将请求传递给继续使用Forms的ASP.NET。

The following is a basic implementation of such module. 以下是此类模块的基本实现。 It will return a 401 Unauthorized and ask the user to login to the realm WOPR . 它将返回401 Unauthorized并要求用户登录领域WOPR It will then accept the password Joshua , ignoring the username specified. 然后它将接受密码Joshua ,忽略指定的用户名。

Compile this as a separate assembly and add it to your web.config, in the system.web/httpModules and system.webServer/modules sections. 将其编译为单独的程序集,并将其添加到web.config, system.web/httpModulessystem.webServer/modules部分。 No other modification of your web application is required. 无需对Web应用程序进行其他修改。

using System;
using System.Text;
using System.Web;

namespace Research {
    public class AuthenticationModule : IHttpModule {
        public void Init(HttpApplication app) {
            app.BeginRequest += (sender, e) => {
                if (!Authenticate(app.Context)) {
                    app.Context.Response.Status = "401 Unauthorized";
                    app.Context.Response.StatusCode = 401;
                    app.Context.Response.AddHeader("WWW-Authenticate", "Basic realm=WOPR");

                    app.Response.ClearContent();
                    app.Context.Response.End();
                }
            };
        }

        public void Dispose() {
        }

        public static Boolean Authenticate(HttpContext context) {
            var authHeader = context.Request.Headers.Get("Authorization");
            if (String.IsNullOrEmpty(authHeader))
                return false;

            if (!authHeader.StartsWith("Basic "))
                return false;

            var base64Credentials = authHeader.Substring(6);
            var binaryCredentials = Convert.FromBase64String(base64Credentials);
            var asciiCredentials = Encoding.ASCII.GetString(binaryCredentials);
            if (!asciiCredentials.Contains(":"))
                return false;

            var credentials = asciiCredentials.Split(new[] { ':' }, 2);
            return credentials[1] == "Joshua";
        }
    }
}

There is a simple solution for you since you want to use FORMS authentication... 有一个简单的解决方案,因为你想使用FORMS身份验证...

  1. Add this section to your web.config under the main header. 将此部分添加到主标题下的web.config中。 Do not merge it -- just copy paste it as one block. 不要合并它 - 只需将其粘贴为一个块即可。 The location tag will isolate these rules from the rest of the web.config and allow its easy removal later. 位置标记将这些规则与web.config的其余部分隔离开来,以便以后轻松删除。

      <location allowOverride="false"> <system.web> <authentication mode="Forms"> <forms loginUrl="frontdoor.aspx" name=".ASPXFORMSAUTH"> </forms> </authentication> <authorization> <deny users="?" /> </authorization> </system.web> </location> 
  2. Create a page called "frontdoor.aspx". 创建一个名为“frontdoor.aspx”的页面。 Get the contents of that aspx page from this link: http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx (scroll down to where it says: "To create the logon page") 从以下链接获取该aspx页面的内容: http//msdn.microsoft.com/en-us/library/xdt4thhy.aspx (向下滚动到它所说的位置:“创建登录页面”)

  3. ALL DONE! 全部做完! This will lock your entire site and allow you to specify the username and password (I call this process "locking the front door") for the front door independently from the rest of the site. 这将锁定整个站点,并允许您指定前门的用户名和密码(我将此过程称为“锁定前门”),与站点的其余部分无关。 The front door credentials are specified in the front door file itself (not very secure but good enough for what you (we) need) in this conditional: 前门凭证在前门文件中指定(不是很安全,但足以满足您(我们)需要的条件):

    [Line 6:] [第6行:]

     If ((UserEmail.Text = "TheSharedFrontDoorLogonName") And (UserPass.Text = "AndItsPassword")) Then 

Modify the conditional to suit your needs and then e-mail your clients/business-types the credentials to the front door. 修改条件以满足您的需求,然后通过电子邮件将您的客户/业务类型的凭据输入到前门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM