Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, im working on a ASP.net project were i have bumped in to some errors this i probablly a rookie misstake here is what im trying to do:

As you can see i have two "returns" how should i do this i really would like to have this random password thing =)

You can see my error here it's a picture:


protected void b1_Click(object sender, EventArgs e)
        {

            // RANDOM password

            {
                const string consonnants = "bcdfghjklmnpqrstvwxz";
                const string vowels = "aeiouy";

                string passwordz = "";
                byte[] bytes = new byte[4];
                var rnd = new RNGCryptoServiceProvider();
                for (int i = 0; i < 3; i++)
                {
                    rnd.GetNonZeroBytes(bytes);
                    passwordz += consonnants[bytes[0] * bytes[1] % consonnants.Length];
                    passwordz += vowels[bytes[2] * bytes[3] % vowels.Length];
                }

                rnd.GetBytes(bytes);
                passwordz += (bytes[0] % 10).ToString() + (bytes[1] % 10).ToString();
                return passwordz;
            }


            // Variabler blir vad användaren matar in i formuläret

            first = TB1.Text;
            last = TB2.Text;
            description = TB3.Text;
            telephoneNumber = TB4.Text;
            username = first.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower() + last.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower();

            // Connect to LDAP  

            DirectoryEntry myLdapConnection = createDirectoryEntry();




            createUser(myLdapConnection, domain, first, last, description, telephoneNumber, password, username, true);


    
    

            // Skickar vidare användaren till en ny sida "klart"
            Response.Redirect("~/klart.aspx");


        }



        static int createUser(DirectoryEntry myLdapConnection, String domain, String first,
                              String last, String description, String telephoneNumber, object[] password,
                              String username, bool enabled)
        {
            // create new user object and write into AD  

            DirectoryEntry user = myLdapConnection.Children.Add(
                                  "CN=" + first + " " + last, "user");

            // User name (domain based)   
            user.Properties["userprincipalname"].Add(username + "@" + domain);

            // User name (older systems)  
            user.Properties["samaccountname"].Add(username);

            // Surname  
            user.Properties["sn"].Add(last);

            // Forename  
            user.Properties["givenname"].Add(first);
       
            // Display name  
            user.Properties["displayname"].Add(first + " " + last);

            // Description  
            user.Properties["description"].Add(description);

            // E-mail  
            user.Properties["mail"].Add(username + "@" + domain);

            // TelePHÅNE
            user.Properties["telephoneNumber"].Add(telephoneNumber);
            
            user.CommitChanges();

            // set user's password  

            user.Invoke("SetPassword", password);

            // enable account

            if (enabled)
                user.Invoke("Put", new object[] { "userAccountControl", "512" });  

            user.CommitChanges();


            return 0;
        }
Posted

You can't do this:
C#
protected void b1_Click(object sender, EventArgs e)
        {
            ...
                return passwordz;

A void method cannot return a value of any sort.
And if you define a method that returns a value then:
1) It cannot be used as the handler for an event, because the signature will not match.
2) It must either return a value on all paths, or throw an exception. You cannot retun a value on one branch, and not on another:
C#
protected void b1_Click(object sender, EventArgs e)
        {
 
            // RANDOM password

            {
...
                return passwordz;
            }
 

            // Variabler blir vad användaren matar in i formuläret
...
            Response.Redirect("~/klart.aspx");
 

        }
The compiler will (quite rightly) not allow that.
 
Share this answer
 
What should i do then =) ?I really need this function, i could to it reallt easy and to it like this:

object[] password = { "12345678ADWdddw" };

and then user.Invoke("SetPassword", password);

But this will set a static password, i would really like a randomized.
 
Share this answer
 
It looks like you think you need a return to get the passwordz value out of the scope where it is built. You don't.
Where are you declaring the object[] password that you pass to the createUser(...) ??

You don't need for the password to be an object[].
Read up on the params keyword. That is the way to pass a variable number of values to a method.
So, change the createUser(...) to just take a string password parameter and set it directly.
C#
protected void b1_Click(object sender, EventArgs e)
{
  // RANDOM password
  const string consonnants = "bcdfghjklmnpqrstvwxz";
  const string vowels = "aeiouy";
 
  // avoid the string concatenations
  System.Text.StringBuilder passwordz = new System.Text.StringBuilder();
  byte[] bytes = new byte[4];
  var rnd = new RNGCryptoServiceProvider();
  for (int i = 0; i < 3; i++)
  {
      rnd.GetNonZeroBytes(bytes);
      passwordz.Append(consonnants[bytes[0] * bytes[1] % consonnants.Length])
               .Append(vowels[bytes[2] * bytes[3] % vowels.Length]);
  }
  rnd.GetBytes(bytes);
  passwordz.Append((bytes[0] % 10).ToString()).Append((bytes[1] % 10).ToString());
  string password = passwordz.ToString();

  // Variabler blir vad användaren matar in i formuläret
  first = TB1.Text;
  last = TB2.Text;
  description = TB3.Text;
  telephoneNumber = TB4.Text;
  username = first.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower() + last.Replace('å', 'a').Replace('Å', 'A').Replace('ä', 'a').Replace('Ä', 'A').Replace('ö', 'o').Replace('Ö', 'O').Replace('ü', 'u').Replace('Ü', 'u').Replace('é', 'e').Replace('è', 'e').Replace(" ", "").ToLower();
 
  // Connect to LDAP  
  DirectoryEntry myLdapConnection = createDirectoryEntry();
  createUser(myLdapConnection, domain, first, last, description, telephoneNumber, password, username, true);
 
  // Skickar vidare användaren till en ny sida "klart"
  Response.Redirect("~/klart.aspx");
}
  
static int createUser(DirectoryEntry myLdapConnection, String domain, String first,
                      String last, String description, String telephoneNumber, String password,
                      String username, bool enabled)
{
    // create new user object and write into AD  
    DirectoryEntry user = myLdapConnection.Children.Add(
                          "CN=" + first + " " + last, "user");
    // User name (domain based)   
    user.Properties["userprincipalname"].Add(username + "@" + domain);
    // User name (older systems)  
    user.Properties["samaccountname"].Add(username);
    // Surname  
    user.Properties["sn"].Add(last);
    // Forename  
    user.Properties["givenname"].Add(first);
    // Display name  
    user.Properties["displayname"].Add(first + " " + last);
    // Description  
    user.Properties["description"].Add(description);
    // E-mail  
    user.Properties["mail"].Add(username + "@" + domain);
    // TelePHÅNE
    user.Properties["telephoneNumber"].Add(telephoneNumber);
    user.CommitChanges();   // Why .CommitChanges() before you're done?
    // set user's password  
    user.Invoke("SetPassword", password);
    // Instead of Invoke, why not:
    user.Password = password;
    
    // enable account
    if (enabled)
        // user.Invoke("Put", new object[] { "userAccountControl", "512" });  // new object[]... is unnecessary
        user.Invoke("Put", "userAccountControl", "512");  
    user.CommitChanges();
    return 0;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900