adding/setting a cookie vb.net - asp.net

I know how to add / set cookies in VB.Net , usually I check if it is null ( or Nothing in VB) if it is nothing I set a new cookie , if it is not then I set the value of the previous cookie. My question is there any thing wrong , or any down side to just adding a cookie every time like this:
HttpContext.Current.Response.Cookies.Add(New HttpCookie("Lat", dt(0)(1).ToString().Trim()))
I'm hoping that this will just override the previous Cookie("Lat") if it exists , and set a new one if it doesn't , if this works it will really make my code a lot shorter and make things easier. I don't see why this wouldn't work - but every tutorial and example online normally checks if it exists first.

If you want to add (duplicate) cookie into the collection then use Add method and want to update an existing cookie then call the Cookies.Set method. (Reference MSDN)

Related

Loosing session variable data from one page to the other in VB.NET

I am a bit new to VB.NET. I have a page that sets 2 session variables and does a redirect to second page. The second pages is at least using one of the session variables. I can tell because on the second page, if the session variable is not correct the user is redirected to an access denied page. The second page also uses the session variable in question. It will read it an fill a gridview based on the value of the variable. I set the variable like so
Session("ID") = Convert.ToInt32(a_value)
and on the second page I retrieve the variable like this
a_page_variable = Session("ID")
What I find strange is that when I run this code in visual studio it works as expected but when I deploy and run it, I get 0 from my session variable instead of the true value of "a_value". I have tried a few things like making sure the data types match up from page to page and trying different ways to retrieve the variable such as
Session("userID")
and
CType(Session.Item("userID"), Int32)
I've also tried to see what is coming in to the second page by using
Response.Write
I also tried to use SQL Profiler to see what kind of call is being made to fill the gridview but I haven't had any luck. The gridview gives me an empty dataset and the Profiler does not detect a call being made from the application. I thought working with session variables was pretty straight forward but obviously, I am missing something.
Thanks for your help,
Billy
One possibility (and the only one that could be guessed at with how little information we have) could be the response.redirect causing the application to terminate due to an exception.
When redirecting, you want to always pass a false, and then call complete request.
Response.Redirect(urlstring, False)
Response.CompleteRequest()
not following these steps can cause exceptions, which may drop session.
Additionally, resolve virtual paths, as some browsers (mobile especially) can see those redirects as new requests entirely, thus generating new session tokens.
Dim urlstring As String
urlstring = Page.ResolveUrl("~/default.aspx")
that said, there are a number of possible causes for this situation.
Application Pool restarts
App Domain restarted
Code changing value unexpectedly
AV tinkering with files
deployed to web farm
With the description provided above, we just don't have enough information to really troubleshoot.
Thank you ADyson, Stephen Wrighton and everyone else who took a stab at helping me figure this out. I was able to find out what was going on by adding code that wrote to a log file on the server. Found the logging code here. I found that I never reached the code that set the session variable and that is the reason it never populated on the second page. I was trying to get the logon name for the user by using Environment.UserName which will return the user name of the person who is currently logged on to the operating system. But what I really wanted to do was get the logon name of the user that was visiting my site. For this I used User.Identity.Name. This works great when you need to know which user from an Active Directory domain is visiting your site.

Pass parameter from 1 page to another using post method

I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)

Pass a URL Parameter to repopulate page

I currently have a page that is populated using a filter, which passes the parameters of the filter via URL.
I'd like to catch those parameters and give the user the opportunity to return to the page with the same filter applied to their view. Essentially, it's a back button. AKA: pass the parameters back into the URL so it repopulates the page.
I hope that made sense. Any help is appreciated! Thank you!
You could store the filter values in a session variable or in a cookie.
Session variable:
Session("MyCustomFilter") = "FilterValue"
Cookie:
Dim cookie As HttpCookie
cookie = New HttpCookie("MyCustomFilter")
cookie.Values.Add("MyCustomFilter", "FilterValue)
While a session variable is easier to use, it will not be saved if you close the browser, while cookies will be saved and can be used later on.
Note: Added a more precise answer, based on my comment.

Session unique on taking new tab in IE7

I provide sessionstate in my web.config file like this
sessionstate mode="InProc" cookieless="UseUri
That way each tab generates a new unique session ID in the URL with the format like this :
http://www.domain.com/(S(kbusd155dhzflbur53vafs45))/default.aspx
It worked, but when I copy the url and paste it on another tab then the previous session value is inheriting. How can I solve this issue? Is there anyother method to solve issue?
A possible solution to this situation would be issue a ticket (guid or seomthing like that) in each response you write to the client. In the request the client would send this ticket and the server would 1) Check to see if it is valid and 2) Invalidate it so just one request (the original one) could be made with it. This way your user wouldn't be able to take advantage of new tabs or even copy/paste of URLs.
If the user pastes a URL containing an existing session token into a new tab, your application cannot possibly know that this is a new tab and not an existing tab. I'm afraid that short of some hacky browser plugin there isn't much you can do about this.

Adding a condition in the url

I would line to add a test condition in an asp.net form such that:
1) From page1.aspx I manually add a query string parameter so that I can trigger the rest of the process in test mode like so: page1.aspx?test=true . This flag must be added in the query string.
2) When I click on a asp.net button in page1.aspx, I am redirected to page2.aspx in test mode
because of teh attached querystring
It seems that I have to work around the postback model of asp.net this is not very straight forward.
Any idea how I can achieve the above behavior?
Thanks
It sounds like you're using a form that posts, but you want to stay in "test" mode. That is, you're not using HTTP-GET so it's not realistic to pass QS variables around.
What I'd do is stash a variable in your Session to set the user's session test mode. So adding &test=true would trigger a Session["TestMode"] = true; before you move to the next page.
Try this (in server-side code)
Response.Redirect("Page2.aspx?Test=" + Request.QueryString["Test"]);

Resources