I am trying to read cookie without post back. I used WebMethod to save the cookie, but cannot find way to retrieve it. For some reason, I cannot find Request.Cookies collection so that I can retrieve the value of it. Any help will be appreciated!
use this in your web method
HttpContext.Current.Request.Cookies
Related
I want to pass an object between pages of my website. I know I can use session object for this.
Personally, I don't want to use any session or application or caching for this.
Is there any other alternative?
Please help.
Thanks,
Mahesh
Another alternative would be to serialize the object into a hidden field and when you post to the other page deserialize the value back to an object instance. Or yet another possibility would be to persist the object to some data store and then use an id to fetch it back.
I have a System.Web.UI.Page page which receive both POST and PUT HTTP requests.
Reading a POST data through the Request.Form field is fine.
But how can i get PUT data? Request.Form field is empty.
Is there a way to access the raw request? or better: The PUT data?
I found the solution,
I was not using the good Field as Put data are contained in the body.
To access put data use:
Request.InputStream;
how can i send object from page to page in asp.net?
Have you thought about using a Session variable? You can save your object to a session varible and then retrieve it from the called page.
Here's an example:
Set from one page
Session["myobject"] = MyObject;
Retrieve from another
MyObject o = Session["myobject"]
You can use the Session to store a user-specific object between pages, or send data via a form GET or POST. There is also PreviousPage for a cross-page postback.
Session
Database
Context Cache
XML/Text File
Post/Get
Azure
Querystring
Pure willpower ;)
The problem is I redirect to a sub page from the main page by passing a few values in Request.
I need to get back the value I got from manipulating values I sent in request and I don't need to store value in Session.
You can use the PreviousPage property, depending on how you got to the new page.
There are a number of things you can do:
Page.Request["ID_Of_Control_You_Are_Interested_In"] will get you a value that you need.
If you Server.Transfer to the page you can call Context.Handler, cast it to the page you come from and traverse the controls collection to get your value.... (similar to PreviousPage property)
You can also inspect the Query string parameters sent in the request too:
Page.Request.QueryString("Param_To_Inspect")
you can return your values as JSON as the response and then using a jquery to get the data
You have three options: Query String Parameters, Cookies and Session State
Query String Parameters: http://www.aspnet101.com/2007/11/using-the-querystringparameter/
This is a good way to pass data between pages. It's the most commonly used and is limited to the max length of the url.
Cookies : http://msdn.microsoft.com/en-us/library/ms178194.aspx
This is a good method. It also has a size limitation. The advantage of cookies is you can persist data that can span visits to your site.
Session State : http://msdn.microsoft.com/en-us/library/ms178581.aspx
This an ok method, I'd use the two methods above before I would use session state. In my experience you can get into trouble very fast with Session state.
To answer your question: You can take the value from the request and load it into a TextBox. Then on the web page, the user edits the value and clicks a save button, which posts the changed value back to the server.
Or you could use javascript to post the value back to the server.
For a website I am building, I am using jQuery to post data to Generic Handlers I created for this purpose.
Because you need to be logged in, to do most post actions (like 'rate a picture'), I am currently using the following technique:
User visits page
Page determines if user is logged in
On Page_Load the page fills a hidden field with an encrypted string, which contains several needed variables, like User ID, Picture ID (of the picture they are currently viewing), the DateTime when the page was rendered.
When the user clicks a "I like this picture"-button, I do a $.ajax post to my Generic Handler, with the encrypted string and the value whether or not they liked the picture.
The Generic Handler decrypts the supplied encrypted string and takes a look at the DateTime to determine if it was not too long ago
When everything works out, the vote is submitted to the database.
In my understanding this is a pretty secure way to handle a situation like this. But maybe I am missing a very important point here.
Any advice would be very welcome.
Looks okay to me.
It looks like you're using a token pattern, a good method with which to secure stateless communications.
You should remember that $.ajax calls to your Handler will also support native ASP.NET security mechanism. When you perform $.ajax the .ASPXAUTH cookie is being add to cookies collection, so you can check in your Handler for this cookie, check HttpContext.User and limit access to the path of your handler in web.config - this all works.