Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

Tamper Proof Query String

Rate me:
Please Sign up or sign in to vote.
4.18/5 (20 votes)
22 Feb 2005CPOL3 min read 195.1K   1.1K   65  
Shows how to prevent/detect that string data was changed.
<%@ Page Language="VB" %>
<script runat="server">

	Sub Page_Load(ByVal source As Object, ByVal e As EventArgs)

		If Not IsPostBack() Then
			'Redirect to same page is not a post back

			'Default value if data is not in query string
			Dim DataString As String = "!!No data passed!!"
			
			If Request.QueryString("Data") IsNot Nothing Then

				Try
					'Decode the query string data
					DataString = DanielHac.TamperProofString.QueryStringDecode(Request.QueryString("Data"))

				Catch ex As Exception
					'Data was tampered with.
					DataString = "!!Data was corrupt!!"
				End Try

			End If

			'Show data on web page
			ShowData.Text = DataString
		End If
	End Sub

    Sub Submit_Click(source as object, e as eventargs)
        'Redirect to this page with the data from InputData
		Response.Redirect("TamperProofQueryString.aspx?Data=" & DanielHac.TamperProofString.QueryStringEncode(InputData.Text))
    End Sub
	
	
	



</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <b>Data received from query string:</b>
        <br />
        <asp:TextBox id="ShowData" runat="server" ReadOnly="True" TextMode="MultiLine" Rows="2" Columns="80"></asp:TextBox>
        <br />
        <br />
        <b>Enter data to be transmitted by query string:</b>
        <br />
        <asp:TextBox id="InputData" runat="server" Text="Test Data"></asp:TextBox>
        <asp:Button id="Submit" onclick="Submit_Click" runat="server" Text="Submit"></asp:Button>
        <br />
        <br />
        <!-- how to use as hyperlink -->
        <a href='TamperProofQueryString.aspx?Data=<%=DanielHac.TamperProofString.QueryStringEncode("This data was stored in the hyperlink.")%>'>HyperLink With Predefined Data</a>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions