ASP.NET/C#: Using Custom Password Generation Script

I would like to use the following code to generate a password 8 characters in length.
http://madskristensen.net/post/Generate-random-password-in-C.aspx

This code returns an error:

<%@ Page Language="C#"%>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<%

public static string GenerateRandomString(int length) 
{ 
            //Removed O, o, 0, l, 1 
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; 
            string allowedNumberChars = "23456789"; 
            char[] chars = new char[length]; 
            Random rd = new Random(); 

            bool useLetter =  true; 
            for (int i = 0; i < length; i++) 
            { 
                if (useLetter) 
                { 
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)]; 
                    useLetter = false; 
                } 
                else 
                { 
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)]; 
                    useLetter = true; 
                } 

            } 

            return new string(chars); 
}

Response.Write("<p>Random Password: " & GenerateRandomString(8) & "</p>");

%>
</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

unknown_routineCommented:
a what line you get the error and what is the error?


I Set up a project and ran your code , it works for me without errors.
0
hankknightAuthor Commented:
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: CS1519: Invalid token '(' in class, struct, or interface member declaration

Source Error:


Line 34: }
Line 35: 
Line 36: Response.Write("<p>Random Password: " & GenerateRandomString(8) & "</p>");
Line 37: 
Line 38: %>

Source File: \\fs6-n01\stor17wc2dfw1\544272\web\content\blast\Pass.aspx    Line: 36 

Open in new window

0
unknown_routineCommented:
OK Now I understand the issue:


First You are using ASP.NET C# , therefore you need to put the function in <Script> Tag Not <%>.

Then In response write you need to use + not & . & is used in Vb.net.

Here is the full code:
<%@ Page Language="C#"%>
<html>
<head>
<title>Hello World!</title>
</head>
<body>

<script language="C#" runat="server">

   
        
        
public static string GenerateRandomString(int length) 
{
   
            //Removed O, o, 0, l, 1 
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; 
            string allowedNumberChars = "23456789"; 
            char[] chars = new char[length]; 
            Random rd = new Random(); 

            bool useLetter =  true; 
            for (int i = 0; i < length; i++) 
            { 
                if (useLetter) 
                { 
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)]; 
                    useLetter = false; 
                } 
                else 
                { 
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)]; 
                    useLetter = true; 
                } 

            } 

            return new string(chars); 
}  
    
</script>


<% Response.Write("<p>Random Password: " + GenerateRandomString(8) + "</p>"); %>




</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.