有什么替代方法可以使用global.asax? [英] what alternatives are there to using global.asax?

查看:503
本文介绍了有什么替代方法可以使用global.asax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IIS中使用自己的自定义身份验证,我希望每个页面上的服务器加载(无论是什么类型的文件),首先检查Application变量以查看用户是否经过身份验证并有权查看该网站。在global.asax中,这可能是:

I am using my own custom authentication with IIS, and I want the server on every page load (no matter what type of file) to first check the Application variable to see if the user is authenticated and authorized to see the site. In global.asax this could be:

void Application_Start(Object Sender, EventArgs e)
{
  if(Application["username"] == null)
  {
    Response.redirect("login.aspx");
  }
}

问题是该网站有多个子根。也就是说, http://example.com/site1 http://example.com/site2 。因此,我想说Application_Start函数在site1上工作但不影响site2。

The problem is that this site has multiple sub-roots. That is, http://example.com/site1 is a completely different website from http://example.com/site2 . Therefore, I would like said Application_Start function to work on site1 but not affect site2.

如果global.asax可以在目录级别自定义,那么这不是问题。但由于每个服务器只有一个global.asax ,我无法实现此解决方案。

If global.asax was customizable at directory level, then this wouldn't be a problem. But since there is only one global.asax per server I cannot implement this solution.

global.asax有哪些替代方案?或者每个目录的global.asax会有所不同吗?

What alternatives are there to global.asax? or can global.asax be different somehow for each directory?

推荐答案

HttpModules是global.asax的替代



(另见 https://www.codeguru.com/csharp/.net/net_asp/article.php/c19389/HTTP-Handlers-and-HTTP-Modules-in-ASPNET .htm

http ://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx

HttpModules在web.config中注册;方便地, 可以在目录级别进行自定义。因此,每个目录都可以拥有自己独特的模块集(在较低的目录中继承)。所有模块都具有与global.asax相同的功能。

HttpModules are registered in web.config; which, conveniently, is customizable at directory level. So every directory can have its own unique set of modules (that are inherited in lower directories). All modules have the same functionality as those found in global.asax .

基本上,每个页面请求都会通过每个注册的模块传递获取实际的页面代码本身。无论发生什么样的请求,都会发生这种情况:

Basically, every page request gets passed through every registered module before it ever gets to the actual page code itself. This happens regardless of what kind of request it is:


"page.aspx"    "page.html"
    |             |
(   |  module 1   |  )
(   |  module 2   |  )
(   |  module 3   |  )
    V             V
(handler 1) (handler 2)

((更好的图片和描述可以在 https://www.codeguru.com/csharp/.net /net_asp/article.php/c19389/HTTP-Handlers-and-HTTP-Modules-in-ASPNET.htm ))

((a much better picture and description can be found at https://www.codeguru.com/csharp/.net/net_asp/article.php/c19389/HTTP-Handlers-and-HTTP-Modules-in-ASPNET.htm ))

那么你所需要的一切要做的是将代码定义为模块而不是global.asax。如果用户未经过身份验证,则: response.redirect(login.aspx)将阻止控件进入处理程序并解析/返回/运行请求的页面。

So then all you need to do is define your code as a module instead of in global.asax . If the user isn't authenticated, then: response.redirect("login.aspx") will stop control from ever reaching the handler and parsing/returning/running the requested page.

这比这复杂一点,所以在codeguru网站上可以找到更好的描述/教程。

It's a little more complicated than that, so a better description/tutorial can be found at the codeguru website.

这篇关于有什么替代方法可以使用global.asax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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