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.
protected void b1_Click(object sender, EventArgs e)
{
const string consonnants = "bcdfghjklmnpqrstvwxz";
const string vowels = "aeiouy";
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();
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();
DirectoryEntry myLdapConnection = createDirectoryEntry();
createUser(myLdapConnection, domain, first, last, description, telephoneNumber, password, username, true);
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)
{
DirectoryEntry user = myLdapConnection.Children.Add(
"CN=" + first + " " + last, "user");
user.Properties["userprincipalname"].Add(username + "@" + domain);
user.Properties["samaccountname"].Add(username);
user.Properties["sn"].Add(last);
user.Properties["givenname"].Add(first);
user.Properties["displayname"].Add(first + " " + last);
user.Properties["description"].Add(description);
user.Properties["mail"].Add(username + "@" + domain);
user.Properties["telephoneNumber"].Add(telephoneNumber);
user.CommitChanges();
user.Invoke("SetPassword", password);
user.Password = password;
if (enabled)
user.Invoke("Put", "userAccountControl", "512");
user.CommitChanges();
return 0;
}