abhijit
abhijit

Reputation: 1968

Encrypt the querystring in C#.net

string emailfield=txtEmail.Text.ToString();
string url = 
   "http://localhost:3076/user/Authenticate-Users.aspx?email="+emailfield;

I want to encrypt the querystring and then decrpyt. Is there any way to do this in C#?

Thanks

Upvotes: 4

Views: 16415

Answers (4)

Jonathan Wood
Jonathan Wood

Reputation: 67195

You can encrypt a name/value collection to a string, and then just pass that encrypted string as a single query argument.

I demonstrate this technique in an article, Encrypting Query Arguments.

Upvotes: 8

Joel Beckham
Joel Beckham

Reputation: 18656

A simpler solution could be to store a GUID along with the user account when it is created. You could call it VerificationCode, for example. When you create the user account, you randomly store a GUID with it, 120a9c10-4f2e-11e0-b8af-0800200c9a66 for example.

Now, in the activation link, you embed the GUID instead of the email address: http://localhost:3076/user/Authenticate-Users.aspx?code=120a9c10-4f2e-11e0-b8af-0800200c9a66

When the page executes, it looks up the user by the GUID to mark that the account has been confirmed.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101604

Possibly looking for Server.UrlEncode?

The URLEncode method applies URL encoding rules, including escape characters, to a specified string.

(Just in case you were too specific with "encrypt", otherwise others have good answers regarding protecting the string's value.)

Upvotes: 0

Tedd Hansen
Tedd Hansen

Reputation: 12314

Since encrypted data will most likely contain special characters it must be base64-encoded or similar.

You can find a encode / decode class that does the dirty work for you. Many of them out there. Here is one example.

Upvotes: 2

Related Questions