gpt4 book ai didi

c# - 如何在 ashx 文件上运行自定义 System.Attribute

转载 作者:行者123 更新时间:2023-12-02 14:29:30 25 4
gpt4 key购买 nike

通常,在常见的 aspx 文件上,我可以在页面开头使用 System.Attribute,例如:

    [AuthorizePage()]
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}

public class AuthorizePage : System.Attribute
{
public AuthorizePage()
{
//do some stuff to authorize
}
}

在页面初始化之前,属性的构造函数会运行并执行一些操作以确保用户当前已登录,否则属性构造函数会将用户重定向到登录页面。

我想在 HttpHandler(ashx 文件)上执行相同的操作,但属性从不在 ashx 页面上初始化。

[AuthorizePage()]
public class AjaxHandler : MuCustomClassBase, IHttpHandler, IReadOnlySessionState
{
//The interface implementations and some other custom private methods
}

我对此 ashx 页面进行 AJAX 调用。这可能是属性不运行的原因吗?或者还有其他我必须知道的事情吗?

最终,我会非常高兴知道如何在 ashx 文件上运行自定义 System.Attribute?

最佳答案

假设您使用 ASP.Net 身份验证,您只需将 .ashx 添加到 web.config 中的 protected 页面列表中,IIS/ASP.Net 将处理其余的事情:

  <location path="AjaxRequests.ashx">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>

如果您使用自建身份验证方案,则可以重写 OnProcessRequest 并在该方法中执行必要的身份验证,并根据需要进行重定向。

关于c# - 如何在 ashx 文件上运行自定义 System.Attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com