微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 自定义身份验证的HttpModule如何与Windows身份验证交互?

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

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

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

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

但是因为我想在应用程序级别使其更易于配置,所以我想使用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属性.

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

有人可以建议一个解决方案吗 – 这是因为我的模块在Windows Auth模块之前运行,或者是什么?

…或者,也许有一种更简单的方法来执行我尝试使用IIS或文件系统权限的操作?

解决方法

您需要AuthenticateRequest事件.

AuthenticateRequest event

原文地址:https://www.jb51.cc/csharp/92166.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐