自定义身份验证的HttpModule如何与Windows身份验证交互? [英] How would an HttpModule for Custom Authentication interact with Windows Authentication?

查看:238
本文介绍了自定义身份验证的HttpModule如何与Windows身份验证交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义HttpModule来控制哪些用户可以查看网站。

I am trying to create a custom HttpModule which controls which users can view a site.

我正在尝试利用Windows身份验证来执行此操作。

I am trying to leverage Windows Authentication to do this.

在单个页面上,我可能会这样做:

On an individual page, I would probably do something like this:

if (HttpContext.Current.User.Identity.Name.Contains("jsmith"))
{
    Response.Write("You do not have the correct permissions to view this site.");
    Response.End();
}

但是因为我想在应用程序级别更加可配置,我会喜欢使用HttpModule。

But because I want to make this more configurable at the application level, I would like to use an HttpModule.

这是我对代码的开始:

using System;
using System.Web;

public class CustomAuthHttpModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
        context.EndRequest += new EventHandler(OnEndRequest);
    }

    void OnBeginRequest(object sender, EventArgs e) { }

    void OnEndRequest(object sender, EventArgs e)
    {
        HttpApplication appObject = (HttpApplication)sender;
        HttpContext contextObject = appObject.Context;

        if (contextObject.User.Identity.Name.Contains("jsmith"))
        {
            contextObject.Response.Clear();
            contextObject.Response.End();
        }
    }
}

我可以使用我有的代码,如果我可以把它放在OnBeginRequest()函数中。但是在OnEndRequest()运行之前,不会在HttpContext对象中创建User属性。

I would be fine with using the code I have, if I could put it in the OnBeginRequest() function. But the User property is not created in the HttpContext object until OnEndRequest() runs.

先前运行代码会阻止应用程序执行生成此输出的额外工作,因为有些用户最终会被阻止访问。

Running the code earlier would prevent the application from doing the extra work of producing this output, since some users are just going to be blocked from access in the end.

有人建议解决这个问题吗 - 这是因为我的模块在Windows Auth之前运行模块,或什么?

Can someone suggest a solution to this - is this happening because my module is running before the Windows Auth module, or what?

...或者,也许有一种更简单的方法可以做我想用IIS或文件系统权限做的事情?

... or, maybe there is an easier way to do what I am trying to do with IIS or file system permissions?

推荐答案

您想要 AuthenticateRequest 事件。

AuthenticateRequest活动

这篇关于自定义身份验证的HttpModule如何与Windows身份验证交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆