Safe Way To Encode A Cookie Value In C#?
Apr 23, 2010When storing a value in a cookie using C#, what is the best way to encode (or escape) the value so that it can be retrieved and decoded/unescaped reliably?
I'm not talking about encryption.
When storing a value in a cookie using C#, what is the best way to encode (or escape) the value so that it can be retrieved and decoded/unescaped reliably?
I'm not talking about encryption.
Is it safe to use such code?
Response.Cookies[cookieName].Path = Request.ApplicationPath + "/";
I want to know about all corner cases.
I am busy building a shopping cart with cookies. I have datalist which I populate from the cookies with a delete button next to each cookie
[Code]....
Now the problem is that when I hit the delete / remove button to expire the cookie, what happens when repopulating the datalist is that it shows the original cookie with all it's values as well as a new entry where all the values are blank.
I want to change the value in a cookie:
HttpCookie hc = new HttpCookie("HiddenColumns");
hc.Value = customView.HiddenFields;
hc.Expires = DateTime.Now.AddDays(365);
Response.SetCookie(hc);
Or this way:
Response.Cookies["HiddenColumns"].Value = customView.HiddenFields;;
Response.Cookies["HiddenColumns"].Expires = DateTime.Now.AddDays(365);
But when I retrieve the cookie value, it is still old, unless I do postback. I don't want to use Redirect.
I'm trying to use a webservice that first expects the clients to login, to retrieve a cookie to re-use.
This is done through a login(string user, string pass) method on the webservice.
Doing this through a browser works fine, we get a cookie, and we can see the cookie via Fiddler or whatvever proxysniff thingy.
Time to do the same in ASP.Net, so we use the WSDL and generate a nice proxy class, and it works fine to call the login() method, but Never Ever does a cookie get set !
I already used the "cookiejar" technique - which means i create an instance of a CookieContainer and assign it to the proxyclass like this;
var cookies = new CookieContainer(3);
There is not a clear category on the site where to post this, so giving it a shot here.
Has anyone used VS 2008? I current user Turtoise for some projects, and Source Gear Valut for others.
The company where I work is thinking about moving to VSS because of the MSDN subscription.
My experience with VSS prior to VSS 2005 was that it conied the name "Visual Source Unsafe" and I know first hand that it trashed my work more that once and I stopped using it. Source Gear Valut on the other hand is rock solid.
So is VSS 2008 Really "safe" was VSS 2005 "safe" ?
I am a bit baffled here; using IE7, ASP.NET 2.0 and Cassini (the VS built-in web server; although the same thing seems to be true for "real" applications deployed in IIS) I am looking for the session-id-cookie. My test page shows a session id (by printing out Session.SessionId) and Response.Cookies.Keys contains ASP.NET_SessionId. So far so good.
But I cannot find the cookie in IEs cookie-store! Nor does "remove all cookies" reset the session (as it does in FF)... So where - I am tempted to write that four letter word - does IE store that bloody cookie? Or am I missing something? By the way there is no hidden field with a session id either, as far as I can see. If I check in FF there is a cookie called ASP.NET_SessionId as I would expect. And as mentioned above deleting that cookie does start a new session; as I would expect.
i'm trying to encode an url with the code below;
var encodedUrl = HttpUtility.UrlEncode("http://www.example.com");
var decodedUrl = HttpUtility.UrlDecode("http%3A%2F%2Fwww%2Eexample%2Ecom%2F");
I'm working with the google webmaster tools api and this api expects an URL as shown in the decodedUrl variable above. Every single character is encoded there.
When i use the httputility encode function i get the following result;[[URL] How can i use the encoding variable in such a way that every character in the url is encoded?
The URL link below will open a new Google mail window. The problem I have is that Google replaces all the plus (+) sign in the email body with blank space. It looks like it only happens with the + sign. ( I am working the ASP.NET web page)
[URL]
(In the body email, "Hi there+Hello there" will show up as "Hi there Hello there")
I know that I could use HttpServerUtility.UrlTokenDecode Method to do the job. But the problem is that I am using .NET 1.1 and this method is only supported in .NET 2.0+. Also I found that Convert.ToBase64String method is not an option because of the differences addressed here. So what other options do I have? Do I have to write my own converting method?
View 1 RepliesI want to reliably and easily Uri encode paths such as "/folder/foo%bar" to "/folder/foo%25bar".
I would have hoped that HttpUtility.UrlPathEncode would do the trick but this method only escapes spaces. I don't want to url HttpUlity.UrlEncode as this will encode "foo bar" to "foo+bar" - which ain't what I want.
I need HTML ENCODE in JavaScript (client side) a String (where User could insert HTML TAGS) from a TextBox so bypassing Reqeust.Validation.Javascript should Encode string and Display it Encoded in Label.
<asp:TextBox ID="uxValueInput" runat="server"></asp:TextBox>
<br />
<asp:Label ID="uxResultEncoded" runat="server" Text="Label"></asp:Label>
<asp:Button ID="uxEncodeButton" runat="server" Text="Button" />
I am new in JavaScript and I have tried different scripts on a web but with no success.Could you please post a really simple example so I would be able to understand how could work
I have the following code to encode password feild but it gets error when password feild greater than ten characters.
private string base64Encode(string sData)
{
try
[code]...
i have a requirement to encode the url in javascript and in redirected page i can't use javascript so i must decode the url back in code. so i needs a javascript encode method which encodes url like URLEncode method of asp.net so that i can decode it in code with UrlDecode method
View 3 RepliesI used the following code to "Encode" my select parameters prior to sending data to a sever :
[Code]....
And I know that I should use this for other server actions Updates, deletes.
How do I Decode the values that I sent to server ?
And what about other controls on my page like text boxes, labels, should they encoded as a matter of standard practice ?
I know this is an XSS risk but for my sepcific scenario am willing to accept it.
I want to use an ASP.NET label control in suh a way that I give it some HTML and it renders it as html without being encoded.
I have a listview in a User Control. It uses FreeTextBox for one of the fields.
I do not want to utilize
[Code]....
Here is my code from the listview edit item template:
[Code]....
What else can I do to not have the validate request set to false and still get beyond the error?
I have a text in the DB:<B>My new text</b> is a good text.
I want "My new text" was as Bold style. But I have original text on my site (<B>My new text</b> is a good text). How I can make text format from DB?
Code how I show data:[Code]....
Have an issue with the HTML editor. Whenever somebody uses an apostophe ( ' ) in the editor and then clicks save (which will send an update to the database) it is taking the apostrophe as part of my command line and it is returning an error. I used a replace to change the apostophe into an encoded html tag but then it was changing it for all the <div> and <span> created by the editor which was causing none of the code to work. how I can encode the apostrophe without encoding the code? Here's my code:
[Code]....
I am opening a popup window on a javascript like this:
openPop("/directory/File.aspx?name=" + strSession,
"550", "800")
I want to encode a Session variable that is in strSession. I have tried the following options but when the popup opens the variable is always visible, unencoded, in the browser address window.
strSession = HttpUtility.HtmlEncode(strSession)
strSession = Server.UrlEncode(strSession)
While saving password i am encrypting it using the following method:Â
public string base64Encode(string sData) { try { byte[] encData_byte = new byte[sData.Length]; encData_byte = System.Text.Encoding.UTF8.GetBytes(sData); string encodedData = Convert.ToBase64String(encData_byte); return encodedData; } catch (Exception ex) { throw new Exception("Error in base64Encode" + ex.Message); } }
As I've mentioned in other threads, I have zilch experience configuring iis 5.1.
So I've been having some troubles with things that work perfectly on the vs webserver, but when I upload the project, they cease to work.
I have a project where I use expression encoder to encode video files to a format compatible with the silverlight media player . The thing is, when I encode the uploaded videos with expression in the vs bundled webserver, it encodes them Okay, no problem.
But once I upload the project, it just encodes .wmv files. I suppose it's got to be something with permissions, as When I'm working with the vs webserver I can see the ffdshow filters activate in the system tray when the videos are encoding. in the iis versions no such thing happens.
I am trying to download an external image and encode it, using the function below.
[code]....
I've installed a Free TextBox editor to allow clients to do a write up about themselves and style it with HTML. Been reading about cross site script attacks (XSS) and want to make sure I'm correctly uploading the data collected with the Server.encode method.
Is this the right way to do it?:
[code]....
I have a Gridview and I try to Iterate through the NewValues collection and HTML encode all.
I am following MSDN CODE.... using their code (posted here) I receive an error:
Collection was modified; enumeration operation may not execute.
I would like ask you a Full simple example how to implement it, so beginners like me can start use this function.
PS: I posted a similar questions here and people replied but I still do not understand it and i Would need a simple example.
[code]....