Asked by:
How to get a supplied module into an ASP.NET site so I can test it

Question
-
User-1394491597 posted
Hi,
I'm very new to ASP.NET but have been able to grasp and use the very basics. However, I have been supplied with the following, which is for encrypting a string in a specific way, and am struggling to build this into an ASP.NET page for testing.
--
Imports System
Imports System.IO
Imports System.Linq
Imports System.Security.Cryptography
Imports System.Text
Module Program
Sub Main(args As String())
Dim response As String = EncryptString("Hello World - Encoded", "DBB2884AF4FF40BF9E4E8E97A1152A8E")
Console.WriteLine(response)
End Sub
Private Function EncryptString(ByVal plaintext As String, ByVal key As String) As String
If String.IsNullOrWhiteSpace(key) Or key.Length < 32 Then _
Throw New ArgumentException(NameOf(key), "Must be 32 characters (256 bits) long")
If String.IsNullOrWhiteSpace(plaintext) Then _
Throw New ArgumentNullException(NameOf(plaintext))
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim sha As SHA256 = SHA256.Create()
Dim iv(15) As Byte
Using rnd As RandomNumberGenerator = RandomNumberGenerator.Create()
rnd.GetBytes(iv)
End Using
AES.IV = iv
AES.Key = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
AES.Mode = Security.Cryptography.CipherMode.CBC
Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = Encoding.UTF8.GetBytes(plaintext)
Return Convert.ToBase64String(AES.IV.Concat(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)).ToArray())
End Function
End Module--
Can someone explain in basic terms how to go from this script to a working demo page that displays the encrypted version of "Hello World - Encoded" please, because my attempts to do it are not working?
Many thanks
Monday, May 11, 2020 2:33 PM
All replies
-
User1535942433 posted
Hi DPJ101,
Accroding to your description and codes,I have create a test.Could you tell us what the asp.net page means?You need webform?MVC?or WebApI?
Since you don't tell us what the asp.net page you want,I create a demo of webform.You need to create a control like label or textbox in the front page.Then in the code-behind,you need to call the EncryptString in the page-load and assign value to foreground control.
WebForm:
aspx.page:
<div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div>
code-behind:
Imports System.Security.Cryptography Public Class _2166832 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim response As String = EncryptString("Hello World - Encoded", "DBB2884AF4FF40BF9E4E8E97A1152A8E") Label1.Text = response.ToString() End Sub Private Function EncryptString(ByVal plaintext As String, ByVal key As String) As String If String.IsNullOrWhiteSpace(key) Or key.Length < 32 Then _ Throw New ArgumentException(NameOf(key), "Must be 32 characters (256 bits) long") If String.IsNullOrWhiteSpace(plaintext) Then _ Throw New ArgumentNullException(NameOf(plaintext)) Dim AES As New System.Security.Cryptography.RijndaelManaged Dim sha As SHA256 = SHA256.Create() Dim iv(15) As Byte Using rnd As RandomNumberGenerator = RandomNumberGenerator.Create() rnd.GetBytes(iv) End Using AES.IV = iv AES.Key = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key)) AES.Mode = Security.Cryptography.CipherMode.CBC Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor Dim Buffer As Byte() = Encoding.UTF8.GetBytes(plaintext) Return Convert.ToBase64String(AES.IV.Concat(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)).ToArray()) End Function End Class
Result:
Best regards,
Yijing Sun
Tuesday, May 12, 2020 3:14 AM -
User-1394491597 posted
Hi Yijing Sun,
Yes, ultimately what I want to end up with is a web form where various things are entered then on pressing a button these are combined into a single string and encrypted.
At this stage though I just need to get the actual encryption part running for me so I know I can do that part.I tested your code but I must still be missing something. Could you please post the full code you have for your aspx and aspx.vb pages?
I tried it 2 ways and got different sets of errors.1. USING CODEBEHIND IN THE PAGE DECLARATION
-----------------------------------------------------------------
enctest.aspx page...<%@ Page Language="VB" AutoEventWireup="false" CodeBehind="enctest.aspx.vb" Inherits="ProjectName.enctest" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>enctest.aspx.vb page...
Imports System.Security.Cryptography
Public Class _2166832
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim response As String = EncryptString("Hello World - Encoded", "DBB2884AF4FF40BF9E4E8E97A1152A8E")
Label1.Text = response.ToString()
End Sub
Private Function EncryptString(ByVal plaintext As String, ByVal key As String) As String
If String.IsNullOrWhiteSpace(key) Or key.Length < 32 Then _
Throw New ArgumentException(NameOf(key), "Must be 32 characters (256 bits) long")
If String.IsNullOrWhiteSpace(plaintext) Then _
Throw New ArgumentNullException(NameOf(plaintext))
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim sha As SHA256 = SHA256.Create()
Dim iv(15) As Byte
Using rnd As RandomNumberGenerator = RandomNumberGenerator.Create()
rnd.GetBytes(iv)
End Using
AES.IV = iv
AES.Key = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
AES.Mode = Security.Cryptography.CipherMode.CBC
Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = Encoding.UTF8.GetBytes(plaintext)
Return Convert.ToBase64String(AES.IV.Concat(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)).ToArray())
End Function
End ClassParser error is
Parser Error Message: Could not load type 'ProjectName.enctest'.I also tried it with just Inherits="enctest" and got a similar error
2. USING CODEFILE IN THE PAGE DECLARATION
-------------------------------------------------------------When I create a new web form in Visual Studio it sets it up with PAGENAME.aspx and PAGENAME.aspx.vb files. The page declarion in the aspx page uses CodeFile like this...
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PAGENAME.aspx.vb" Inherits="PAGENAME" %>
In this attempt my enctest.aspx page code was...
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="enctest.aspx.vb" Inherits="enctest" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>and the content of the enctest.aspx.vb was the same as in example 1
This way there are various errors in Visual Studio
Error 1 'Context' is not a member of 'enctest'. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx 1 1 C:\...\enctest\
Error 2 'Context' is not a member of 'enctest'. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx 1 1 C:\...\enctest\
Error 3 'Label1' is not declared. It may be inaccessible due to its protection level. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx.vb 8 9 C:\...\enctest\
Error 4 'NameOf' is not declared. It may be inaccessible due to its protection level. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx.vb 13 37 C:\...\enctest\
Error 5 'NameOf' is not declared. It may be inaccessible due to its protection level. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx.vb 16 41 C:\...\enctest\
Error 6 'Security' is ambiguous, imported from the namespaces or types 'System.Web, System'. C:\Users\family\Documents\Visual Studio 2010\Projects\enctest\enctest.aspx.vb 30 20 C:\...\enctest\
And viewing it in a browser gives me the following error
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Source Error:
Line 1: Imports System.Security.Cryptography
And the detailed errors are many, mostly like…
error BC30456: 'Context' is not a member of 'enctest'.I'm sure I'm doing something very basic wrong here! If you can post the full code of your working test, that will probably highlight the issue for me, thanks.
Tuesday, May 12, 2020 9:07 AM -
User1535942433 posted
Hi DPJ101,
Accroding to your description and codes,as far as I think,Inherits corresponds to your project.aspx name.Example,vb is my project name and exctest is my webform page name.
If you copy from a Web application to Website,the namespace you need to use codefile instead of codebehind.
More details,you could refer to below codes;
enctest.aspx.page:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="enctest.aspx.vb" Inherits="vb.enctest" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
enctest.aspx code-Behind:
Imports System.Security.Cryptography Public Class enctest Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim response As String = EncryptString("Hello World - Encoded", "DBB2884AF4FF40BF9E4E8E97A1152A8E") Label1.Text = response.ToString() End Sub Private Function EncryptString(ByVal plaintext As String, ByVal key As String) As String If String.IsNullOrWhiteSpace(key) Or key.Length < 32 Then _ Throw New ArgumentException(NameOf(key), "Must be 32 characters (256 bits) long") If String.IsNullOrWhiteSpace(plaintext) Then _ Throw New ArgumentNullException(NameOf(plaintext)) Dim AES As New System.Security.Cryptography.RijndaelManaged Dim sha As SHA256 = SHA256.Create() Dim iv(15) As Byte Using rnd As RandomNumberGenerator = RandomNumberGenerator.Create() rnd.GetBytes(iv) End Using AES.IV = iv AES.Key = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key)) AES.Mode = Security.Cryptography.CipherMode.CBC Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor Dim Buffer As Byte() = Encoding.UTF8.GetBytes(plaintext) Return Convert.ToBase64String(AES.IV.Concat(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)).ToArray()) End Function End Class
Best regards,
Yijing Sun
Wednesday, May 13, 2020 2:11 AM -
User-1394491597 posted
Hi Yijing,
I don't have this set up as a project, merely the 2 files enctest.aspx & enctest.aspx.vb
Does that matter? Are there other files/folders I need to make this work?
Just changing the page instruction as described gives me the following parser error...
Parser Error Message: Could not load type 'vb.enctest'.
Source Error:
Line 1: <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="enctest.aspx.vb" Inherits="vb.enctest" %>
Many thanks
Wednesday, May 13, 2020 8:16 AM -
User753101303 posted
Hi, leave Inherits="ProjectName.enctest".
Make sure to copyy/paste thje content of the class but not the class name ie you had :
ublic Class _2166832 ' was Likeley enctest, it needs to match the name for the companion page Inherits System.Web.UI.Page
Also if you had a namespace statement leave it alone (if I remember VB uses a default namespace).
In short you need to copy the "core" of the sample code but need to leave unchanged things such as the class name of the page you created on your side.
Wednesday, May 13, 2020 10:11 AM -
User-1394491597 posted
Hi Patrice,
Still getting errors that way but changing the Page declaration to this seems to get me further...
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="enctest.aspx.vb" Inherits="enctest" %>
This is how Visual Studio wants to set it up anyway when I create a new web form page so happy to run with that.
The error I now get is...Compiler Error Message: BC30451: 'NameOf' is not declared. It may be inaccessible due to its protection level.
Source Error:
Line 15: Line 16: If String.IsNullOrWhiteSpace(key) Or key.Length < 32 Then _ Line 17: Throw New ArgumentException(NameOf(key), "Must be 32 characters (256 bits) long")
Feels like there's something pretty basic I'm missing and wish I understood this better than I do.
Wednesday, May 13, 2020 10:36 AM -
User1535942433 posted
Hi DPJ101,
Accroding to your description,as far as I think,the problem is related with your vb version,The NameOf operator is available in Visual Basic 14 and later.
You could tell us what you vb version?I suggest you could change in the web.config.
More details,you could refer to below codes:
Web.config:
<compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers>
More details,you could refer to below articles:
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/nameof
https://github.com/aspnet/RoslynCodeDomProvider/issues/16
Best regards,
Yijing Sun
Thursday, May 14, 2020 7:34 AM