Difference Between HttpApplication Class And IHttpModule?
Jan 31, 2011What is the difference between HttpApplication class and IHttpModule? Are they both same or different?
I see articles that mention the same events in both the classes.
What is the difference between HttpApplication class and IHttpModule? Are they both same or different?
I see articles that mention the same events in both the classes.
I am getting the following error
'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' at the following code (line in bold or double **)
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
**app.EndRequest += new EventHandler(Application_EndRequest);**
}
protected void Application_EndRequest(object sender, EventArgs e)
{
UnitOfWork.Commit();
}
which is mentioned in Global.asax file.
public class NHibernateSessionPerRequest<T>:IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
context.EndRequest += EndRequest;
}
}
How can it be registered in web.config?
I set up various global parameters in Global.asax, as such:
Application["PagePolicies"] = "~/Lab/Policies.aspx";
Application["PageShare"] = "/Share.aspx";
Application["FileSearchQueries"] = Server.MapPath("~/Resources/SearchQueries.xml");
I have no problem accessing these variables form .ascx.cs or .aspx.cs file -- ie. files that are part of the Web content. However, I can't seem to access 'Application' from basic class objects (ie. standalone .cs files). I read somewhere to use a slight variations in .cs files, as follows, but it always comes throws an exception when in use:
String file = (String)System.Web.HttpContext.Current.Application["FileSearchQueries"];
I want to create my own HttpApplication class, wherein I can handle methods like ExecuteStep. let me know if there are any examples to do this. Kindly provide some pointers to this...
I am using VS 2008. Basically I want to handle all events from httpApplication class (even before global.asax) before it reaches web pages inside Do other classes come into picture if I want to create my own HttpApplication class .
I have class say: Public Class GlobalFA : Inherits System.Web.HttpApplication
which has been inherited from HttpApplication class and having
Public Shared MaxLoginAttempts As Integer = 0 as member variables
Now i have added below event in it i.e(Application_Start event.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
MaxLoginAttempts = 100
end sub
Now i am access this variable on my xyz.aspx.vb page but i am geeting 0 value .
it mean application_start event is not firing .
am currently working on a web application, whereby I want to add code to the Application_BeginRequest method of the Global.asax file, without adding code to the Global.asax file, which sounds a little crazy, let me explain a bit more.If anyone has ever developed in sitecore before, they would have seen that the Global.asax file has empty methods, however it has 'using Sitecore;' and the global.asax file provided does not inherit from System.Web.HttpApplication.
View 1 Repliesit seems classes and structure are same. what is the basic difference between Class and Structure?
View 10 RepliesWhat is the difference between ViewPage and WebViewPage in ASP.NET MVC?
View 1 Repliesi trying to inject js to page (to tags) by using ihttpmodule.but js isn't injected.
the page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTempProject._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
[Code]....
All implementation of IHttpModule I've seen looks following:
[code]....
I am wondering why is the Dispose method always empty? Shouldn't we unsubscribe the event which we subscribe in the Init method?
I've declared an event on an HTTP Module so it will poll subscribers for a true/false value to determine if it should go ahead with its task of tweaking the HTTP Response. If only one subscriber answers true then it runs its logic.
Are there potential pitfalls I'm not seeing?
public class ResponseTweaker : IHttpModule {
// to be a list of subscribers
List<Func<HttpApplication, bool>> listRespondants = new List<Func<HttpApplication, bool>>();
// event that stores its subscribers in a collection
public event Func<HttpApplication, bool> RequestConfirmation {
[Code]....
An IHttpModule implementation I created
public class ResponseTweaker : IHttpModule { // my module ...
is registered in Web.config
<system.web>
<httpModules>
<add name="redman" type="ResponseTweaker"/>
</httpModules>
and an instance of it is sitting in the pipeline.
From the perspective of a caller (e.g. from the Global.asax.cs file), how should I get a reference to that module instance?
LoginPage.aspx:-
protected void Button1_Click(object sender, EventArgs e)
{
Context.Items["Username"] = txtUserId.Text;
Context.Items["Password"] = txtPassword.Text;
//
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Context.Items["Username"].ToString(), DateTime.Now, DateTime.Now.AddMinutes(10), true, "users", FormsAuthentication.FormsCookiePath);
// Encrypt the cookie using the machine key for secure transport
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket
// Set the cookie's expiration time to the tickets expiration time
if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx");
}
Global.asax file:-
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id =
(FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
// Get the stored user-data, in this case, our roles
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
Response.Write(HttpContext.Current.User.Identity.Name);
Response.Redirect("Default.aspx");
}
}
}
}
I get the following error after signing in This webpage has a redirect loop.
The webpage at [URL] has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
I need to quickly write a test console app for testing purposes.This app is basically the console version of an ASP.NET app.The original ASP.NET app makes use of the global Application object for storing global data.How can I get the same functionality of the Application object in my console app? Can I declare an HttpApplication object directly and use it?NOTE - this app is just for debugging some issues, not production code, so I'm ok if it's not "best practice".
View 3 RepliesIm trying to make a solution like this [URL] Now the question is how do I filter the request, I only want to Open an ISession if the request is for an ASPNET MVC Action, not for *.gif, *.css etc. How should I handle this filtering?
View 3 RepliesI've used the table at the top of this article as a reference. I have three questions:
1 - Can multiple users (from different physical locations) ever share an HttpApplication instance? If so, does this happen by default?
2 - Can multiple users (from different physical locations) ever share an HttpApplicationState instance? If so, does this happen by default?
3 - Can multiple users of an ASP.NET application ever share a singleton instance or a static variable value? If so, does this happen by default?
How HttpApplication hood up the Session_Start()? I mean how HttpApplication execute the Session_Start()?
View 1 RepliesIn S#Arch based web application I need to read some data during the application initialization. From the first point, the best place - HttpApplication.Application_Start() or HttpApplication.Init()But, Application_Start isn't applicable as there is no WebSessionStorage yet. Init() seems isn't fit as well, as there is no NHibernateSession.
View 1 RepliesIn ASP.NET 4.0, I have an IHttpModule that apply a filter on HttpRequest.Filter. As the result, the content stream length is changed, and it breaks WCF with now returns 400 bad request because of the mismatch between the body length and the HTTP headers.
View 1 RepliesHow can I access the cache of one web application/domain from another web application/domain?Here is my scenario. I need to verify/check the existence of a certain object in one web application/domain's cache from another web application/domain.
View 1 RepliesI've created a simple sample site to demonstrate the issue. In it, I have a Default.aspx Page that has a button on it:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p><asp:Button OnClick="ButtonClick" Text="Button" runat="server" />
</p>
<asp:Label ID="output" runat="server" />
</asp:Content>
The code behind just sets the label text on the button click:
protected void ButtonClick(object sender, EventArgs e)
{
output.Text = "Button Click!!";
}
I then have an IHttpModule that gets called for every request:
[code].....
And now it's broken again! So to make a long story short, just by accessing the Form collection on the request in the IHttpModule, I somehow screw up the PostBack, and the event never gets fired.
Does anyone have a clue why setting the Principal for the context would be so slow that a request times out? I have a custom HttpModule that subscribes to the "AuthenticateRequest" event. I have this call which works fine to create the Principal (which makes all the DB queries)
[Code]....
where context.User is source.Context.User where source is the HttpApplication.
I hope I am posting into the right area... I want to know if you can access a session variable from a HTTP Module in IIS 7. Here is the scenario, I want to access the session values (i.e. User Object) from a HTTP Module. I have read oodles and tried many things but to no avail. I guess the application uses Forms authentication to authorize a logon to the site. I want to monitor the url that is submitted... RAWUrl property and if they are going to a certain page I want to grab their user object from THEIR session and validate them for access to that page. I want to use a httpmodule for this. The long and short of this is "Can I get access to session variables from the request from the httpmodule code behind? If I can, could you beso kind as to show me a working sample... I have tried many permutations on this using httpapplication, context and so forth and I can't seem to get access to session variables that would belong to the users request. it this possible?
View 1 RepliesI there a way to know if a request is a soap request on AuthenticateRequest event for HttpApplication? Checking ServerVariables["HTTP_SOAPACTION"] seems to not be working all the time.
public void Init(HttpApplication context) {
context.AuthenticateRequest += new EventHandler(AuthenticateRequest);
}
protected void AuthenticateRequest(object sender, EventArgs e) {
app = sender as HttpApplication;
if (app.Request.ServerVariables["HTTP_SOAPACTION"] != null) {
// a few requests do not enter here, but my webservice class still executing
// ...
}
}
I have disabled HTTP POST and HTTP GET for webservices in my web.config file.
<webServices>
<protocols>
<remove name="HttpGet" />
<remove name="HttpPost" />
<add name="AnyHttpSoap" />
</protocols>
</webServices>
Looking at ContentType for soap+xml only partially solves my problem. For example,
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 1131
Content-Type: text/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: ro
Host: localhost
mymethod: urn:[URL]
Some clients instead of having the standard header SOAPAction: [URL], have someting like in example above. "mymethod" represents the method in my web service class with [WebMethod] attribute on it and [URL] is the namespace of the webservice. Still the service works perfectly normal. The consumers use different frameworks (NuSOAP from PHP, .NET, Java, etc).