2

Currently I've got a web application with two pages, Login and Default.

I've been tirelessly trying to write 2x GUID's which are returned into a HTTP header when a login procedure is executed in the code behind on the login page. I then want to read these out in the page load of the default page.

I'm rather inexperienced with HTTP headers in C# and I tried the following earlier to add the values in from login with no success....

Request.Headers.Add("GUID", strGUID);
Response.Headers.Add("GUID", strGUID);

To retrieve the values on the default page I was trying to use...

strGUID = Response.Headers.Get("GUID");

And then to finally clear the header of all the custom crap I'd piled into it, I was trying to use...

Response.Headers.Clear();

I've been looking around the Internet but I can't seem to find any tutorials on this or any step-by-step explanations of how to achieve a similar task.

2
  • 1
    What exactly are you trying to do? It sounds like you want a place to store values for exactly one request. For this, I recommend looking into HttpContext.Current.Items Commented Dec 17, 2012 at 20:43
  • Basically I'm trying to place some data (a token/GUID) in a header on the Login page, so that in the pageload of the default page I can check its validity and redirect the user if it's not valid. Commented Dec 18, 2012 at 9:37

1 Answer 1

4

Since HTTP is a stateless protocol, you can't share data in between requests using HTTP headers. I don't see anything wrong with your code per se, but there's no rule that says an HTTP header sent to the client in one response (login.aspx), should be sent back to the server in the next request (default.aspx). This remains true even when performing a redirect from login.aspx to default.aspx, since a redirect itself is separate from the page view that follows it.

What you are probally looking for is storage that last for more than a single request. For that you can use cookies to store the value in the browser:

// Set in login.aspx
Response.Cookies.Add(new HttpCookie("Guid", Guid.NewGuid())

// Read in default.aspx
HttpCookie token = Request.Cookies["Guid"];

...or session storage to store the value on the server.

// Set in login.aspx
Session["Guid"] = Guid.NewGuid();

// Read in default.aspx
Guid token = (Guid) Session["Guid"];

You can use a tool like Fiddler to inspect HTTP traffic to better understand how HTTP headers are transmitted.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.