【问题标题】:Is it possible to execute a function at IIS for authentication before hits URLs?是否可以在 IIS 上执行功能以在点击 URL 之前进行身份验证?
【发布时间】:2019-01-30 03:10:50
【问题描述】:

我们的应用程序托管在 IIS 上,层次结构如下:

MainAppn1
---subAppn1
---subAppn2
---subAppn3

是否可以在 IIS 上自动执行一个功能,以便在用户点击 url 时对所有子应用程序进行常见的身份验证(例如,http://server1/MainAppn1/subAppn1.aspx。最好的答案将不胜感激!

【问题讨论】:

  • 在 ASP.NET 中,您可以将代码放在 Application_BeginRequest 中。在 ASP.NET Core 中,您可以创建一个中间件。
  • 我们使用asp.net webform 4.0。是否有可能在调用任何子应用程序(如 subAppn1、subAppn2、subAppn3)之前放置可执行授权代码(以 dll 或 httpmodule 的形式用于拦截预处理请求)以检查每次页面访问?

标签: asp.net authentication iis web-applications


【解决方案1】:

你可以实现Custom HTTP Module

namespace AspNetWebForm
{
    public class CustomHttpModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += Application_BeginRequest;
            application.AuthenticateRequest += Application_AuthenticateRequest;
            application.AuthorizeRequest += Application_AuthorizeRequest;
        }

        private void Application_BeginRequest(object sender, EventArgs e)
        {    
        }

        private void Application_AuthenticateRequest(object sender, EventArgs e)
        {
        }

        private void Application_AuthorizeRequest(object sender, EventArgs e)
        {
        }

        public void Dispose()
        {
        }
    }
}

web.config

在 IIS 7.0 集成模式下注册 HTTP 模块。

<configuration>
  <system.webServer>
    <modules>
      <add name="CustomHttpModule" type="AspNetWebForm.CustomHttpModule"/>
    </modules>
  </system.webServer>
</configuration>

【讨论】:

  • 非常感谢。我应该在类库中创建这个 httpmodule 类吗?如果是,那么我应该在 Main 文件夹 MainAppn1. 中部署一个 dll 文件吗?请提供在 IIS 上放置此通用 httpmodule 身份验证的步骤?
  • Should I create this httpmodule class in a class library? 这取决于你。在 web.config 中注册您的自定义 HTTP 模块就足够了。您无需在 IIS 中执行任何操作。
  • “你不必在 IIS 中做任何事情”,除非 modules 部分足够松,可以在 web.config 中被覆盖(但并非总是如此)。
  • @Muruga 1) 您可以创建一个单独的项目并在所有项目中引用相同的 DLL 文件或创建一个 nuget 包。 2) 只需将注册放在应用程序的 web.config 文件中即可。
  • @Muruga 不,这在 ASP.NET Web 窗体中是不可能的。您需要继承IHttpModule,尤其是用于身份验证。
猜你喜欢
  • 1970-01-01
  • 2013-10-15
  • 2018-02-14
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2017-07-02
相关资源
最近更新 更多