473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Any downsides to cookie assignment inside custom class using HttpContext.Current? ASP.NET 2.0 cookie fix?

Today I learned that creating cookies inside of a custom class in ASP.NET
2.0 requires that you prefix it with HttpContext.Current..., e.g. :

HttpContext.Current.Response.Cookies.Add("myNewCoo kie");

I am wondering if there are any landmines that I should know about, or if
this will work pretty much as I am expecting a cookie should.

Nov 19 '05 #1
2 2103
This is true in 1.x as well. Request/Response/... are available in
codebehind 'cuz your class inherits from Control (via Page/UserControl)
which exposes this properties. Your custom class, which doesn't inherit
from such as base class doesn't have these things set up (and it shouldn't).
So you hook into the page framework via HttpContext.Current.

Anyways, to get to your question, are there any downside? Well, you are
tying your custom class to the web, but I can't say if that's a downside or
not in this situation. Do you ever plan on reusing this class inside a dos
console, web service or win form?HttpContext.Current will be null in those
situations, so HttpContext.Current.Response will throw a null reference
exception. There's nothing wrong with building a this type of dependency,
but the class shouldn't be considered part of your business layer
anymore...it is now part of your presentation layer.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
<ke*****@nospam.nospam> wrote in message
news:uI**************@TK2MSFTNGP10.phx.gbl...
Today I learned that creating cookies inside of a custom class in ASP.NET
2.0 requires that you prefix it with HttpContext.Current..., e.g. :

HttpContext.Current.Response.Cookies.Add("myNewCoo kie");

I am wondering if there are any landmines that I should know about, or if
this will work pretty much as I am expecting a cookie should.

Nov 19 '05 #2
Thanks for Karl's informative inputs.

Hi kenfine,

I think Karl's suggestions are quite reasonable. The behavior is just the
same no matter in asp.net 1.x or 2.x. Also, it's better that we guarantee
the component is used in asp.net web application only since the HttpContext
won't exists in other desktop or service app.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net>
| References: <uI**************@TK2MSFTNGP10.phx.gbl>
| Subject: Re: Any downsides to cookie assignment inside custom class using
HttpContext.Current? ASP.NET 2.0 cookie fix?
| Date: Tue, 16 Aug 2005 22:53:15 -0400
| Lines: 38
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Response
| Message-ID: <OI**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 206-248-153-84.dsl.teksavvy.com 206.248.153.84
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118483
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| This is true in 1.x as well. Request/Response/... are available in
| codebehind 'cuz your class inherits from Control (via Page/UserControl)
| which exposes this properties. Your custom class, which doesn't inherit
| from such as base class doesn't have these things set up (and it
shouldn't).
| So you hook into the page framework via HttpContext.Current.
|
| Anyways, to get to your question, are there any downside? Well, you are
| tying your custom class to the web, but I can't say if that's a downside
or
| not in this situation. Do you ever plan on reusing this class inside a
dos
| console, web service or win form?HttpContext.Current will be null in
those
| situations, so HttpContext.Current.Response will throw a null reference
| exception. There's nothing wrong with building a this type of
dependency,
| but the class shouldn't be considered part of your business layer
| anymore...it is now part of your presentation layer.
|
| Karl
|
| --
| MY ASP.Net tutorials
| http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
| http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
| come!)
|
|
| <ke*****@nospam.nospam> wrote in message
| news:uI**************@TK2MSFTNGP10.phx.gbl...
| > Today I learned that creating cookies inside of a custom class in
ASP.NET
| > 2.0 requires that you prefix it with HttpContext.Current..., e.g. :
| >
| > HttpContext.Current.Response.Cookies.Add("myNewCoo kie");
| >
| > I am wondering if there are any landmines that I should know about, or
if
| > this will work pretty much as I am expecting a cookie should.
| >
| >
| >
|
|
|

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom attribute is attached to the underlying page...
6
by: hansiman | last post by:
When I use the sub code below directly in the code behind page it works. However, if I move the code into a sub and place the sub in a class file I get a building error "Name 'Response' is not...
1
by: Earl Teigrob | last post by:
When I write inline code on a aspx page using Page.Response.Cookies() to read a cookie, everything works fine, as in the following code. Dim x As Object = Request.Cookies("thing") If x Is Nothing...
0
by: Bill Borg | last post by:
Hello all, Not sure I can describe this adequately, but I am creating an endless loop when an authenticated user signs out. When the user signs out, I want to remove authentication, abandon...
2
by: Keith Patrick | last post by:
I'm trying to programmatically post data to another page within my ASP.Net app. Not POSTing is not an option (I can't store this data in my session, context, hidden fields, or anything else...I've...
3
by: Steve Franks | last post by:
Is there a way I can extend the HttpContext or one of its subclasses to include a property that exposes a custom class of mine to all ASP.NET pages? More specifically, I'd like to use a...
6
by: Denise | last post by:
I have an application that sets a cookie through a class. I set it in a push button event on a postback. When the page reloads, a user control checks the value of that cookie to display name...
4
by: =?Utf-8?B?YW5vb3A=?= | last post by:
hello, I am writing the Following coding for preventing Session Fixation attack in ASP.Net website, but I could not retrieve the cookie added and the value of cookie_value remains blank. ...
0
by: =?Utf-8?B?YW5vb3A=?= | last post by:
hello, I am writing the Following coding for preventing Session Fixation attack in ASP.Net website, but I could not retrieve the cookie added and the value of cookie_value remains blank. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.