10

I just want to write code inside my assembly to detect whether it is running on a "desktop machine", or inside the context of an ASP.NET application. This is crucial guys, (licensing purposes), and I don't want to get fired because I did a mistake.

So, please, be direct and if you please give me some code snippet.

EDIT: I depend on your answers

EDIT v 2.0: What about using in the assembly:

[assembly: "AspNetHostingPermission (SecurityAction.RequestRefuge, Unrestricted=true)]"

to prevent the execution on the web?

2 Answers2

4

This might be helpful:

How determine if application is web application

Community
  • 1
  • 1
Andreas Folkesson
  • 475
  • 1
  • 4
  • 10
  • You're probably right. I didn't search for it and I only saw what was listed after I entered the question. So, I give you the credits. I'll check it. Thank you. –  Jul 06 '10 at 13:04
  • Now that I am thinking: if someone create an executable (console) and call it would the same code still be valid? I don't have experience with ASP.NET and don't know the tricks. –  Jul 06 '10 at 13:09
4

Check whether the HttpContext.Current is null and then report it's not running as a web app, e.g.:

if (HttpContext.Current == null) {
     // Report that it is not running as web app.
}

You will have to make a reference to the System.Web in your references and you using statement.

Turnkey
  • 9,266
  • 3
  • 27
  • 36
  • 1
    You have a point. I merged your proposal with the above (afolkesson) in a statement. –  Jul 06 '10 at 13:24
  • 5
    By experience i should point out that if they check HttpContext from a new thread it will be null, also sometimes it is null even in the current thread...not so reliable as a variable... – VSP Apr 24 '15 at 12:17