About HttpContext
HttpContext provides a means to access certain web-based components when at the page, user control, business object, etc. level. HttpContext is created by the .NET framework so that the IHttpHandler and IHttpModule objects that the framework run on have access to key ASP.NET objects they may need. This class is exposed through the HttpContext.Current property (a static implementation representing the current request). You may notice that the Page object implements some of the same features; in fact, the properties that the Page object duplicates are being returned from the HttpContext object.
With that said, here is how we can use it:
The above code will write a message directly to the response stream, and so we will see the message "This is my message" in the browser. This is one of the great benefits of this class.
Using HttpContext In Components
You can use HttpContext in your components to access security related information. For instance, to get the user name of a person (if authenticated), you can do this:
You can also access the Request and Response streams through:
HttpContext provides you with resources to the HttpServerUtility class (exposed as the Server property), tracing capabilities, application/session information, caching, and much more. It is because HttpContext is instantiated during the lifecycle of the application that it is available in your components store in App_Code or in a class library. The Current property represents the current request, and you have to ensure that the object isn't null before using it. For example, when trying to get the user information of a windows authenticated user, you should do:
About Membership and Roles
In addition to HttpContext, we can also use the Membership and Roles objects to get information about the Membership and Roles of an application. These objects have more requirements; remember that they use a configuration section in the element, containing a and elements as children. Regardless, you can set this up in a web or windows application, which makes it versatile to use throughout your application. Membership and Roles are static classes, so you can do things like:
I was surprised at this first, whenever I saw this in the Microsoft hands-on examples for Enterprise Library security block; but after thinking about it, it is essentially a class and does make very much sense. So you can use it in the web and windows environment; whenever working in the windows environment, you have to use the GetUser() override where you can specify the user name, like I did in my previous example.
Summary
As you can see, the HttpContext and other web objects are accessible to you components exposed in an ASP.NET application, or in the windows environment, providing you with a lot more capabilities in your applications.
References
Check out the HttpContext Members to find out more about what is available. Also, check out this article on DotNet John to see how to use windows accounts for Membership, but application roles with the Roles API.
About Brian Mains
 |
Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare.
In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...
This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Welcome the WebUI Test Studio v2.0!
read more
Introducing Recurring Appointments for Web.UI Scheduler ASP.NET AJAX
read more
November's Toolbox Column Now Online
read more
Web-based Scheduling Solution with the WOW-Factor for ASP.NET 2.0 and Up
read more
Q3 2009 Release Week kicks-off November 4th, Free Daily Webinars
read more
Sneak Peak: CSS Sprites Make Your Websites Faster
read more
ASP.NET 4 Web Server Here Shell Extension
read more
New articles published on Ajax performance and futures
read more
Upgrade Wizard: auto-upgrades and more
read more
Adhoc testing of .NET RIA Services
read more
|
|
Please login to rate or to leave a comment.