我的应用程序中有一个 HttpModule,它使用以下代码连接到 FormsAuthenticationModule 的 Authenticate 事件:
public void Init(HttpApplication context)
{
FormsAuthenticationModule faModule =
(FormsAuthenticationModule)context.Modules["FormsAuthentication"];
faModule.Authenticate +=
new FormsAuthenticationEventHandler(faModule_Authenticate);
}
不幸的是,对 context.Modules 的调用失败了,因为应用程序需要在中等信任的环境中运行。有没有其他方法可以连接到此事件?
最佳答案
这很难——您甚至无法从全局应用程序文件中访问 Modules 集合。
您可以尝试从 Global 中的 AuthenticateRequest 处理程序调用您的自定义代码:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// Call your module's code here..
}
您也无法从集合中获取自定义模块,因此您需要对模块库的静态引用。
除了在机器级 web.config 中向您的站点授予 AspNetHostingPermission ( as detailed for other permissions here ) 之外,我别无他法!
关于asp.net - 如何在中等信任环境中连接到当前的 FormsAuthenticationModule?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/431644/