我想向整个站点添加基本的 HTTP 授权,该站点对某些 protected 页面使用表单例份验证。这个想法是将门户的开发阶段呈现给客户,以便他可以使用它,但要在 HTTP 级别使用额外的简单登录名和密码来保护整个站点,以便其他人无法访问它。
web.config 中的经典标签在这里是不够的,因为门户可以使用表单例份验证(通过用户注册)作为其功能的一部分。
我想在表单例份验证之前通过 HTTP 授权用户,这可能会在以后发生。

HTTP 级别授权应具有管理员配置的登录名和密码(即在文本文件中)。

是否可以通过自定义 http 模块来实现此功能?

更新:

这个想法是创建保护应用程序的配置级方式。 Forms Authentication 不是这里的选项,因为处理不同的帐户类型和角色需要在应用程序中进行更改。

我已经使用简单的自定义模块解决了这个问题,该模块使用 HTTP 401 测试 HTTP 授权 header 和响应。该模块可以通过 Web.Config 附加到任何网站

解决方案:

模块类:

public class BasicAuthModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(this.HttpApplicationBeginRequest);
    }

    private void HttpApplicationBeginRequest(object sender, EventArgs e)
    {
        var request = HttpContext.Current.Request;
        var response = HttpContext.Current.Response;
        string authHeader = request.Headers["Authorization"];
        if (string.IsNullOrEmpty(authHeader))
        {
            this.RequireAuthorization(response);
        }
        else
        {
            string authType = authHeader.Split(' ').First();
            string authData = authHeader.Split(' ').Last();
            if (authType.ToLower() == "basic")
            {
                byte[] bytes = Convert.FromBase64String(authData);
                string plainText = Encoding.UTF8.GetString(bytes);
                string login = plainText.Split(':').First();
                string password = plainText.Split(':').Last();
                if (!this.Validate(login, password))
                {
                    this.DenyAccess(response);
                }
            }
            else
            {
                this.DenyAccess(response);
            }
        }
    }

    private bool Validate(string login, string password)
    {
        return (login == ConfigurationManager.AppSettings["AuthLogin"]) && (password == ConfigurationManager.AppSettings["AuthPwd"]);
    }

    private void RequireAuthorization(HttpResponse response)
    {
        response.AddHeader("WWW-Authenticate", "Basic realm=\"stage\"");
        response.StatusCode = 401;
        response.Status = "401 Authorization Required";
        response.ContentType = "text/html";
        response.End();
    }

    private void DenyAccess(HttpResponse response)
    {
        response.AddHeader("WWW-Authenticate", "Basic realm=\"stage\"")
        response.StatusCode = 401;
        response.Status = "401 Authorization Required";
        response.ContentType = "text/html";
        response.End();
    }
}

在 web.config 中:
<modules runAllManagedModulesForAllRequests="true">
  ...
  <add name="BasicAuthModule" type="MyNamespace.BasicAuthModule, MyNamespace.Module"/>
</modules>

最佳答案

使用自定义模块当然可以做到这一点。您可以(出于其他原因,我也有)忽略 ASP.NET 的用户概念,并让您的模块返回 401,它坚持登录或允许请求继续(视情况而定)。 RFC 2617 提供了您需要了解的所有信息。如果您忽略 ASP.NET 的用户对象,那么标准身份验证和应用程序已经使用的表单例份验证之间不会有任何干扰。

请记住,如果不是通过 HTTPS,basic 很容易被拦截,在这种情况下,您可能希望使用摘要。 Digest 自己做有点复杂,但肯定不是非常困难,并且也记录在 RFC 2617 中。

关于c# - 如何在 .net 应用程序中使用基本的 http 授权和表单例份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5350135/

10-10 21:56