Please Help!

M

momo

Guys I need your help on this.

I have this one problem and I admitted I am a novice at this. This is a Code
Behind in an aspx page. You will see where I have the plus signs below in
SecureQueryString2.vb where I am calling another Class called
"InvalidQueryStringException". My problem is where do I put this code so it
can be called from SecureQueryString2.vb. I have tried to put it in the same
code behind SecureQueryString2.vb and I get errors. How should I do this to
make it work and also check my .ASPX page to make sure I and referencing it
correctlly. Thanks for your help in advance.

Here is the code for InvalidQueryStringException:
==============================
Public Class InvalidQueryStringException
Inherits System.Exception
End Class
==============================

Code Behind named: SecureQueryString2.vb
==============================
Imports System
Imports System.Collections.Specialized
Imports System.Security.Cryptography
Imports System.Text
Imports System.Web

Namespace dma

Public Class SecureQueryString
Inherits NameValueCollection
Private Const cryptoKey As String = "test"
Private ReadOnly IV As Byte() = New Byte(7) {240, 3, 45, 29, 0, 76,
173, 59}

Sub New()
End Sub

Sub New(ByVal encStr As String)
deserialize(Decrypt(encStr))
End Sub

Public ReadOnly Property EncryptedString() As String
Get
Return HttpUtility.UrlEncode(Encrypt(Serialize()))
End Get
End Property

Public Overrides Function ToString() As String
Return EncryptedString
End Function

Private Function Encrypt(ByVal serQS As String) As String
Dim buffer As Byte() = Encoding.ASCII.GetBytes(serQS)
Dim des As TripleDESCryptoServiceProvider = New
TripleDESCryptoServiceProvider
Dim md5 As MD5CryptoServiceProvider = New
MD5CryptoServiceProvider
des.Key =
md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey))
des.IV = IV
Return
Convert.ToBase64String(des.CreateEncryptor.TransformFinalBlock(buffer, 0,
buffer.Length))
End Function

Private Function Decrypt(ByVal encQS As String) As String
Try
Dim buffer As Byte() = Convert.FromBase64String(encQS)
Dim des As TripleDESCryptoServiceProvider = New
TripleDESCryptoServiceProvider
Dim MD5 As MD5CryptoServiceProvider = New
MD5CryptoServiceProvider
des.Key =
MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey))
des.IV = IV
Return
Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer,
0, buffer.Length()))
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Catch ex As CryptographicException
Throw New InvalidQueryStringException
Catch ex As FormatException
Throw New InvalidQueryStringException
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
End Try
End Function

Private Sub Deserialize(ByVal decQS As String)
Dim nameValuePairs As String() = decQS.Split("&")
Dim i As Integer
For i = 0 To nameValuePairs.Length - 1
Dim nameValue As String() = nameValuePairs(i).Split("=")
If nameValue.Length = 2 Then
Me.Add(nameValue(0), nameValue(1))
End If
Next
End Sub

Private Function Serialize() As String
Dim sb As StringBuilder = New StringBuilder
Dim key As String
For Each key In Me.AllKeys
sb.Append(key)
sb.Append("=")
sb.Append(Me(key))
sb.Append("&")
Next key
Return sb.ToString
End Function

End Class
End Namespace



Here is my .ASPX page code:
====================

<%@ Page language="VB" src="SecureQueryString2.vb" AutoEventWireup="false"
Inherits="SecureQueryString" %>
<%@ Import Namespace="dma" %>

<script language="VB" runat="server">
'Sending page
Dim qs
qs = new SecureQueryString()
qs("Name") = "Test"
qs("Phone") = "704-822-8999"
Response.Redirect("Data2.aspx?x=" + qs.ToString())
</script >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
 
S

slagomite

you shouldn't have a problem with this code -- as long as you're
properly referencing whatever namespace your
"InvalidQueryStringException" class is defined within. if you want,
you can put it in the same codebehind file, within your "dma"
namespace, and this code should work fine... otherwise, if it's in a
different namespace, you'd have to reference it accordingly within your
codebehind. e.g.:

=======================
SecureQueryString2.vb
=======================

Namespace dma
Public Class InvalidQueryStringException Inherits System.Exception
End Class

Public Class SecureQueryString
...
Throw New InvalidQueryStringException
...
End Class
End Namespace


--- OR ---


=======================
Functions.vb
=======================

Namespace dmaFunctions
Public Class InvalidQueryStringException Inherits System.Exception
End Class
End Namespace

=======================
SecureQueryString2.vb
=======================

Imports dmaFunctions

Namespace dma
Public Class SecureQueryString
...
Throw New InvalidQueryStringException
// OR "Throw New dmaFunctions.InvalidQueryStringException",
// if you don't include "Imports dmaFunctions"
...
End Class
End Namespace


HTH,
Luke
 
M

momo

Luke, I want to say thank you for helping me with this, I really appricaite
it. I tried the first option and then run the DATA.ASPX file below and I got
this error. It doesn't make sense. SecureQueryString2.vb is in the same
folder with DATA.ASPX.
Server Error in '/' Application.
--------------------------------------------------------------------------------

Parser Error
Description: An error occurred during the parsing of a resource required to
service this request. Please review the following specific parse error
details and modify your source file appropriately.

Parser Error Message: The base type 'SecureQueryString' does not exist in
the source file 'SecureQueryString2.vb'.

Source Error:


Line 1: <%@ Page language="VB" src="SecureQueryString2.vb"
AutoEventWireup="false" Inherits="SecureQueryString" %>
Line 2: <%@ Import Namespace="dma" %>
Line 3:


Source File: d:\inetpub\wwwroot\websites\specialk\test\Data.aspx Line: 1



--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032


+++++++++++++++++++++++++++
Here is the code for DATA.ASPX:+
+++++++++++++++++++++++++++

<%@ Page language="VB" src="SecureQueryString2.vb" AutoEventWireup="false"
Inherits="SecureQueryString" %>
<%@ Import Namespace="dma" %>

<script language="VB" runat="server">
'Sending page
Dim qs
qs = new SecureQueryString()
qs("Name") = "Test"
qs("Phone") = "777-888-9999"
Response.Redirect("Data2.aspx?x=" + qs.ToString())
</script >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>

Any idea why this is not working?

Thanks.
 
S

slagomite

Hrm, not sure why the error says it doesn't exist -- but the true
problem probably lies in the fact that your ASPX page is inheriting
from a class that does not inherit from System.Web.UI.Page (I didn't
notice this problem before). In your ASPX page, you want to inherit a
page class, something like:

<%@ Page Language="VB" src="Data.aspx.cs" AutoEventWireup="False"
Inherits="Data" %>

and in your the Data.aspx.cs codebehind file:

Public Class Data Inherits System.Web.UI.Page
...
End Class

ASPX pages need to inherit from a class that inherits from
System.Web.UI.Page. In other words, your ASPX page shouldn't be
*extending* your SecureQueryString() class -- it should just be *using*
it.

HTH
Luke
 
M

momo

I took out the "Inherits NameValueCollection" and replaced it with "Inherits
System.Web.UI.Page" and tried to execute it. I got a server error. But when
I change it back I still get the previous error.

Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30456: 'Add' is not a member of
'System.Web.UI.Page'.

Source Error:


Line 86: Dim nameValue As String() = nameValuePairs(i).Split("=")
Line 87: If nameValue.Length = 2 Then
Line 88: MyBase.Add(nameValue(0), nameValue(1))
Line 89: End If
Line 90:
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)

Source File: d:\inetpub\wwwroot\websites\specialk\test\SecureQueryString.vb
Line: 88
 
S

slagomite

No. Don't change the inheritance of the SecureQueryString class --
change the inheritance of your page class, in the @Page directive --
the value of the "Inherits" attribute should match the name of your
codebehind class.

Please take another look at the code I wrote out in my last post.

Luke
 
S

slagomite

Also, please note that your ASPX page should *not* use the
SecureQueryString class as its codebehind -- SecureQueryString should
just be a stand-alone class; you should have *another* class as your
codebehind (the name of the file which contains it should be the the
name of your ASPX page, with ".vb" appended -- e.g., if your ASPX page
is called "Data.aspx", your codebehind file should be called
"Data.aspx.vb", and the name of the codebehind class in that file
should be "Data").

Visual Studio should have created such a codebehind file for you -- to
view it, right-click somewhere in your HTML view of the ASPX page, and
click "View Code".

HTH,
Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top