how to block web application based on date in asp.net? - asp.net

I have developed one web application for my client in asp.net. That application has been hosted in the web server. If my client does not satisfy my requirement, I want to block that web application running from the server based on date. How to do that?

You can ether use the Application_BeginRequest to make that test as:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string cTheFile = HttpContext.Current.Request.Path;
string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
// here you can check what file you wish to cut, ether by file, ether by extention
if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
{
// and here is the time limit.
if(DateTime.UtcNow > MaxDateToUse)
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusCode = 403;
HttpContext.Current.Response.End();
return ;
}
}
}
ether add the same test inside a BasePage use that BasePage for all pages, and make there that test as I describe it on this answer: Make an asp .net web app offline
relative:
Making a web site available only for a specified time
switch off the all functions in .net service
Make an asp .net web app offline

Related

How to disable a single aspx page in ASP.NET webapplication?

Would like to disable a single aspx page in an ASP.NET web application and show the message 'This page is under maintenance' while the application is still up. Is this possible?
One way to do that on Application_BeginRequest on global.asax. Check the if the specific file is called and show some other static page and End the process there. Here is how:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string cTheFileName = System.IO.Path.GetFileName(HttpContext.Current.Request.Path);
// if its the file you want to be offline
if (cTheFileName == 'filename.aspx')
{
// the file we look now is the app_offline_alt.htm
string cOffLineFile = HttpRuntime.AppDomainAppPath + "app_offline_alt.htm";
// if exist on root - if not we skip the offline mode.
// this way you can put it offline with just the existing of this file.
if (System.IO.File.Exists(cOffLineFile))
{
using (var fp = System.IO.File.OpenText(cOffLineFile))
{
// read it and send it to the browser
app.Response.Write(fp.ReadToEnd());
fp.Close();
}
// and stop the rest of processing
app.Response.End();
return;
}
}
}
the app_offline_alt.htm contains a simple under maintenance message or what ever you like.

Verify hostname or IP address before request processing ASP.NET MVC

I want to make my ASP.NET MVC 3 web app to run on particular hostname or IP address only. If someone tries to host the site on different host or IP address, the website should stop working as it see the hostname/IP address different than configured (basically, hardcoded in the app DLL).
Any idea how effectively this can be achieved in ASP.NET MVC?
In your Global.asax file create new BeginRequest function:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (application.Request.Url.Host != "mydomain.com")
{
application.CompleteRequest();
}
}
Filter requests using HTTP Module.
Another way to accomplish it
is creating an action attribute to validate that request :P
public class HostValidatorAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
//validate here and returns true if valid
}
}

How do I get the IP address of the current website in global.asax?

I'd like to do this in the Application_Start method, so I can write a 'robots.txt' file if the site is running on a test server.
Thanks
I originally thought old school, using the ServerVariables collection, as the following worked nicely in my test app, but running under the built in VS web server:
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
string localAddress = Context.Request.ServerVariables["LOCAL_ADDR"];
// Returns "::1" when running under the VS server, however it throws an
// exception under IIS Express, so I assume it also does so under IIS.
}
The next best option I can come up with would be something like:
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
string serverName = Server.MachineName;
}
Which works on both VS and IIS express, so if you know the name of your test or live servers, you could check against that instead?
Try this.
var ips = Dns.GetHostAddresses(Dns.GetHostName());
if (ips.Any())
{
var serverIp = ips[ips.Count()-1];
}
Dns.GetHostAddresses will return different length due to local or remote server,
that's why i use ips.Count()-1 to get the least one.
Try this:
HttpRequest httpRequest = HttpContext.Current.Request;
string host = httpRequest.Headers["host"];
IPAddress hostAddresse = Dns.GetHostAddresses("host").Where(a=>a.AddressFamily ==AddressFamily.InterNetwork).FirstOrDefault();

How can I utilize or mimic Application OnStart in an HttpModule?

We are trying to remove the global.asax from our many web applications in favor of HttpModules that are in a common code base. This works really well for many application events such as BeginRequest and PostAuthentication, but there is no Application Start event exposed in the HttpModule.
I can think of a couple of smelly ways to overcome this deficit. For example, I can probably do this:
protected virtual void BeginRequest(object sender, EventArgs e)
{
Log.Debug("Entered BeginRequest...");
var app = HttpContext.Current.Application;
var hasBeenSet app["HasBeenExecuted"] == null ? false : true;
if(!hasBeenSet)
{
app.Lock();
// ... do app level code
app.Add("HasBeenExecuted", true);
app.Unlock();
}
// do regular begin request stuff ...
}
But this just doesn't smell well to me.
What is the best way to invoke some application begin logic without having a global.asax?
Just keep a static bool in the HttpModule:
private static bool _hasApplicationStarted = false;
private static object _locker = new object();
private void EnsureStarted()
{
if (_hasApplicationStarted) return;
lock (_locker)
{
if (_hasApplicationStarted) return;
// perform application startup here
_hasApplicationStarted = true;
}
}
Then have any method that needs the application to have started just call EnsureStarted.
HttpModules and HttpHandlers will execute on every single request, while the Global.asax App Start event is when the application starts, thus only once.
You could make a general global.asax which will load all assemblies with a specific interface, and then drop in the dll's you want executed for that specific application. Or even register them in your web.config, and have your general global.asax read the keys, and then load and execute the code you want.
I think this is better than putting app once code in a module and checking on a state variable.

c# HttpModule to handle pseudo sub-domains

I'm new to development with .NET and working on a personal project. My project will allow users to create their own simple mobile site.
I would like to write a HTTP Module that will handle pseudo sub-domains.
I have already setup my DNS wildcard, so sub.domain.com and xxx.domain.com etc. point to the same application. I want to be able to extract sub and ID parts from sub.domain.com/pageID.html URL and load settings of the page from a database server in order to build and render the page to the client.
I can do it with URL rewrite tools like isapirewrite, but I want my application to be independent from OS so that the server doesn't require installation of any 3rd party app.
Is it possible to do it with HTTP handlers?
Can anyone post an example?
You can check the domain at any time. Where to do it dependings on your application's goals. E.g. if you want to serve different pages depending on the domain you could do like this:
public class MyModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string host = app.Request.Url.Host;
if(host == "first.domain.com")
{
app.Context.RewritePath("~/First.aspx");
}
}
}

Resources