Track each request to the website using HttpModule - asp.net

I want to save each request to the website. In general I want to include the following information:
User IP, The web site url, user-if-exist, date-time.
Response time, response success-failed status.
Is it reasonable to collect the 1 and 2 in the same action? (like same HttpModule)?
Do you know about any existing structure that I can follow that track every request/response-status to the website?
The data need to be logged to sql server.

Look in your web server logs.

How about IIS logs? they already have all the data items you listed so far

In BeginRequest Method you need to write the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
{
//String s = HttpContext.Current.Request.Path;
//HttpContext.Current.RewritePath("Login.aspx");
String referrer = HttpContext.Current.Request.UrlReferrer;
String sourceIP = HttpContext.Current.Request.UserHostAddress;
String browser = HttpContext.Current.Request.UserAgent;
}

Related

Remove query strings from static resources

My website is running on ASP.NET platform and recently i test my website on pingdom and i found the below error.
Resources with a "?" in the URL are not cached by some proxy caching
servers. Remove the query string and encode the parameters into the
URL for the following resources:
https://projectsdeal.co.uk/ScriptResource.axd?d ...
63Nawdr4rAt1lvT7c_zyBEkV9INg0&t=ffffffffe3663df5
https://projectsdeal.co.uk/ScriptResource.axd?d ...
JGTlZFM0WRegQM9wdaZV3fQWMKwg2&t=ffffffffe3663df5
Simple leave it as it is (its not an error !) - you can not remove this query string from resource because this is the id on how to load that resource from asp.net
The message that you get is actually talk for a proxy caching servers - what is a proxy caching server ? a middle computer that cache pages of your site, not the actually client computer - that can hold in cache that page and not bring slower your site in general.
So your client can hold that resource on cache if you set them correctly, and from what I see asp.net take care correctly and you resource are cached just fine - see this screen shot.
Now if you wish to add even more aggressive cache you can use the global.asax and do something like
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string cTheFile = HttpContext.Current.Request.Path;
if (cTheFile.EndsWith("WebResource.axd", StringComparison.InvariantCultureIgnoreCase))
{
JustSetSomeCache(app);
}
}
private static void JustSetSomeCache(HttpApplication app)
{
app.Response.Cache.AppendCacheExtension("post-check=900, pre-check=3600");
app.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(32));
app.Response.Cache.SetMaxAge(new TimeSpan(32, 0, 0));
app.Response.Cache.SetCacheability(HttpCacheability.Public);
app.Response.AppendHeader("Vary", "Accept-Encoding");
}
What is the different ? The second cache is not check the server at all for file change as the asp.net do, you can gain one webserver call.

how to know when application level variable is changed asp.net

I have application level variable stored in a cache in global.asax file:
void Application_Start(object sender, EventArgs e)
{
Application.Lock();
List<MyClassName> myobjects = new List<MyClassName>();
HttpContext.Current.Cache["List"] = myobjects;
Application.UnLock();
}
And I access it and change it in a page:
protected void Page_Load(object sender, EventArgs e)
{
List<MyClassName> myobjects = (List<MyClassname>)HttpContext.Current.Cache["List"];
//change myobjects...
HttpContext.Current.Cache["List"] = myobjects;
}
Question is:
*Suppose we have two users(browsers) running this site at the same time (for example they are chatting). How to notify another user when one user changes myobjects variable?*
We could check myobjects and compare this with old one periodically using timer. But it is not the best way.
So is there better way to notify another user when global variable is changed?
For a webserver,considering the statelessness of http protocol, there is nothing like user or connected user, just a request and its response. When you say, you want to notify a user, you are trying to say that server will initiate the communication which is not possible.
An idea could be, you could use long-polling, i.e. keep the request continuously alive and whenever you want to notify something to user, you feed in the data but again it is not a scalable solution - at least for low resource servers (of course google can afford it)
In order to notify anything, there user must be connected to the server - i.e. the communication must be initiated from client end.
Another alternative is, using sockets. You could write a custom server and make your web application notify the custom server that the value has changed. The custom server in turn will notify the clients using sockets (Web sockets). This is rather complicated mechanism though. You could use websockets provided by html 5 or use addons like flash/java to work out the socket communication.

How to Create Dynamic Sub-domain with ASP.NET?

How can I create a subdomain in an asp.net C# application? I am working with an asp.net portal.
I have my site which redirects all *.domain.com calls to domain.com. What I want to acheive is that first when the user enters a dynamic subdomain name he should be directed to its home page like if user writes www.microsite1.domain.com, then the site should point to page ~/Microsite/Home.aspx?value=microsite1, and when the user accesses www.microsite1.domain.com/about.aspx then i should able to get the argument value1=about.
The first step is to place the subdomain name in the DNS Host Server. To do that you need to manipulate the dns files. For example if you use BIND as DNS server you go and open the text file that keep your DNS configuration eg: "c:\program files\dns\var\mysite.com" and there you add a line as
subdomain.mysite.com. IN A 111.222.333.444
Also you change the ID of the file to give a message to BIND to update the subdomains.
Second step is to redirect the new subdomain to the correct directory. You do that on the protected void Application_BeginRequest(Object sender, EventArgs e) on Global.asax using rewritepath
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.Host.StartsWith("subdomain."))
{
// here you need to find where to redirect him by reading the url
// and find the correct file.
HttpContext.Current.RewritePath("/subdomain/" + Request.Path, false);
}
// .... rest code
}
Its not so easy, not so hard... maybe there are some more minor issues like permissions to write to dns. Also you need to know dns, read the manual about.

Basic facebook web application session info is missing

I made really basic facebook application, which collect permission from user to post data. Some time ago (around New year) it worked fine.
I am using http://facebooktoolkit.codeplex.com, and my code looks like this:
public partial class Facebook : CanvasFBMLBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if(this.Api.Users.HasAppPermission(global::Facebook.Schema.Enums.ExtendedPermissions.publish_stream))
And at this moment I am having exception: parameter uid or session key required
As i understand, I should have some session defined by Facebook and CanvasFBMLBasePage should parse it, and make it possible to use application, but this is not happening.
My application settings (maybe I mis something):
Canvas Callback URL: http://www.domain.com/app/action/facebook.aspx?
Render Method: FBML
Also, I put my IP in server whitelist.
Thanks for help
Have you called the "ConnectToFacebook" method in the api before calling "HasAppPermission"?

At what point in the ASP.NET page/app life-cycle should I redirect a user based on his country?

We have a web app where we need to redirect customers from .com to .co.uk domains based on the geolocation of ther IP address. When/where should I do this?
The content will change slightly based on their location, but I think I can handle that OK. However, if anyone has any comments on the best way to handle this, I'd like to hear those comments as well.
I would use an HttpModule. It's the earliest place to do the redirection.
If you want to do this in application code, I would do this as early as the IP is available, in the Application_BeginRequest handler in global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
var ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//Redirect here...
}

Resources