我有一个 httpmodule,但自从迁移应用程序 .NET 4 以来,一直遇到一些奇怪的行为。BeginRequest 事件不会在某些请求上触发(但在其他请求上会触发) - 到目前为止,我还没有设法找出何时触发和不触发的模式。
EndRequest 事件似乎总是被触发。
但微软的唯一建议似乎是需要在 system.webServer/modules 部分中添加一个条目 - 我已经准备好了。
我对 .net 和 mvc 很陌生 - 有没有人对我如何追踪此问题的原因有任何建议?
非常感谢任何帮助/建议。
我正在使用这篇文章中的以下代码来尝试创建一个自定义 http 模块:
public class BasicAuthenticationModule: IHttpModule
{
public void Init(HttpApplication application)
{
application.AuthenticateRequest += new EventHandler(Do_Authentication);
}
private void Do_Authentication(object sender, EventArgs e)
{
var request = HttpContext.Current.Request;
string header = request.Headers["HTTP_AUTHORIZATION"];
if(header != null && header.StartsWith("Basic "))
{
// Header is good, let's check username and password
string username = DecodeFromHeader(header, "username");
string password = DecodeFromHeader(header, password);
if(Validate(username, password)
{
// Create a custom IPrincipal object to carry the user's identity
HttpContext.Current.User = new BasicPrincipal(username);
} …Run Code Online (Sandbox Code Playgroud) 我创建的IHttpModule实现
public class ResponseTweaker : IHttpModule { // my module ...
Run Code Online (Sandbox Code Playgroud)
在Web.config中注册
<system.web>
<httpModules>
<add name="redman" type="ResponseTweaker"/>
</httpModules>
Run Code Online (Sandbox Code Playgroud)
它的一个实例就是在管道中.
从主叫方(例如,从文件的Global.asax.cs)的角度来看,如何应该我去该模块实例的引用?
这是我对HttpModule的实现:
带模块的文件:
public class HttpModuleRewriter : IHttpModule
{
#region IHttpModule
public void Init(HttpApplication app)
{
app.BeginRequest += ProcessRequest;
}
public void Dispose()
{
}
#endregion
#region Protected Methods
protected void ProcessRequest(object sender, EventArgs e)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
web.config中:
<?xml version="1.0"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="HttpModuleRewriter" preCondition="managedHandler" type="HttpModuleRewriter" />
</modules>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我在HttpModuleRewriter类的'Init'方法中加入了断点.应用程序启动时调用第一次方法...每个页面请求只调用一次模块.
如果我快速请求页面(第二个请求将在第一个请求被处理之前发送),那么方法"初始化"会被少量调用,每个跟随页面的请求会导致2-3次调用我的模块...
为什么?我怎么能避免这种情况?
谢谢.
PS我已经添加了公共构造成HttpModuleRewriter计算引用的数量,并在我的请求,我有5个模块创建...并为每个请求第2个模块实际上是所谓的...但只适用于第一个导航页中,所有后续页面(我已经检查了3个其他页面)模块只被调用一次(只调用了1个实例)...
为什么第一页被处理两次?建议的答案(使用'初始化'标志)也没有帮助.
我有以下内容IHttpModule,我正在试图弄清楚如何从控制器为给定的绝对或相对URL执行操作.
public class CustomErrorHandlingModule : IHttpModule
{
#region Implementation of IHttpModule
public void Init(HttpApplication context)
{
context.Error += (sender, e) =>
OnError(new HttpContextWrapper(((HttpApplication)sender).Context));
}
public void Dispose()
{}
public void OnError(HttpContextBase context)
{
// Determine error resource to display, based on HttpStatus code, etc.
// For brevity, i'll hardcode it for this SO question.
const string errorPage = @"/Error/NotFound";
// Now somehow execute the correct controller for that route.
// Return the html response.
}
}
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
是否可以在 httpmodule 中为变量设置值并读取 contentpage 和 masterpage 中的变量值?
我想用它代替会话。该变量保存用户 ID 的值。
因为我网站上的每个页面都会创建许多不同的数据库连接,所以我认为多一个比拥有会话值更好?!你怎么认为?
我想创建一个可以发展壮大的网站。(总是尝试以这种方式对我的网站进行编程)这就是为什么我想删除一些会话值。
谢谢!
马蒂亚斯。
我在生产环境中收到以下错误
只有在配置文件或Page指令中将enableSessionState设置为true时,才能使用会话状态.还请确保System.Web.SessionStateModule或自定义会话状态模块包含在应用程序配置的configuration\system.web\httpModules部分中.
我现在尝试了以下几件事
machine.config和web.config,都有enableSessionState="true"enableSessionState="true"到页面中web.config<httpmodules>部分中enableSessionState="true"页面指令注意:当我尝试在Dev环境中调试代码时,一切正常.
任何人都可以帮我解决这个问题,我只是无法找到解决方法.
我试图在响应中注入一些额外的标记,但它没有按预期输出.
例如,以下代码将输出:
... </html>CONTENT
private void OnEndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.Write("CONTENT");
}
Run Code Online (Sandbox Code Playgroud)
但我希望在关闭html标记之前输出内容
... CONTENT</html>
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何想法?
我通过实现IHttpModule接口编写了一个自定义HTTPModule.然后我在web.config文件中注册了它
<configuration>
<system.web>
<httpModules >
<add name="AuthHttpModule" type="AuthHttpModule" />
</httpModules>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试访问任何页面时,它会抛出错误
检测到的ASP.NET设置不适用于集成管理管道模式.
注意:我正在使用VS2012和C#.
在IIS7集成模式下,特别是在ASP.NET MVC和.NET4中,所有模块都针对所有请求运行.runAllManagedModulesForAllRequests = "真".使runAllManagedModulesForAllRequests ="false"给我带来了很多问题,直到我感到困惑为止.
所以为了简单起见,让我的模块接受包括静态文件在内的所有请求,但是在像BeginRequest这样的模块中,我只想处理它不是静态文件.如何过滤或检查模块中的这种情况?
我发现这个伟大的小HttpModule加密和解密所有的查询字符串.它可以在这里找到:HttpModule用于查询字符串加密
有一个主要的缺陷,我真的可以使用一些如何解决的输入.在页面的回发中,跳过HttpMethod POST并且QueryString被解密显示.显然这是一个重大的安全风险.
void context_BeginRequest(object sender, EventArgs e)
{
try
{
HttpContext context = HttpContext.Current;
if (context.Request.Url.OriginalString.Contains("aspx") && context.Request.RawUrl.Contains("?"))
{
string query = ExtractQuery(context.Request.RawUrl);
string path = GetVirtualPath();
if (query.StartsWith(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase))
{
// Decrypts the query string and rewrites the path.
string rawQuery = query.Replace(PARAMETER_NAME, string.Empty);
string decryptedQuery = Decrypt(rawQuery);
context.RewritePath(path, string.Empty, decryptedQuery);
}
else if (context.Request.HttpMethod == "GET")
{
// Encrypt the query string and redirects to the encrypted URL.
// Remove if you don't want all query …Run Code Online (Sandbox Code Playgroud) httpmodule ×12
c# ×8
asp.net ×7
.net ×3
asp.net-mvc ×1
events ×1
httpcontext ×1
iis-7 ×1
web-config ×1