Reputation: 5105
Just some background about my application. I am developing an ASP.Net MVC 3 Web App which uses Entity Framework 4.1 for data persistence. My application is tiered in that it has a UI layer, Service layer, Repository layer etc. I also use Unity for my Inversion of Control Container.
When a user registers on my application, I create two random codes (Email and Mobile verification codes) using the StringBuilder. I then assign these two random codes to their appropriate properties in a User Object, see below.
User validateUser = new User();
validateUser.firstName = model.firstName.Trim();
validateUser.lastName = model.lastName.Trim();
validateUser.email = model.Email.Trim();
//Create Email and Mobile Verification Codes
string randomEmailCode = "";
randomEmailCode = _notifyService.GenerateEmailCode();
validateUser.emailVerificationCode = randomEmailCode;
string randomMobileCode = "";
randomMobileCode = _notifyService.GenerateMobileCode();
validateUser.mobileVerificationCode = randomMobileCode;
NotifyService
public string GenerateEmailCode()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
public string GenerateMobileCode()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(3, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
I then add the user to my DBcontext and call the SaveChanges() method to save the new User into my database.
_accountService.AddUser(validateUser);
_accountService.Save();
Now, when I go into my database and find the new User which has been added, the emailVerificationCode and the mobileVerificationCode are both the same. However, if I put a break point on my code at the following line
randomEmailCode = _notifyService.GenerateEmailCode();
And trace through until the Save, and then go check the database for the new User, both emailVerificationCode and the mobileVerificationCode codes are different, as expected.
I cannot understand why when I run the application it inserts the same code for both properties.
Can anyone please help with this?
Thanks.
UPDATE
I did as Jane suggested, ie, put in hard coded values for each property like so
validateUser.emailVerificationCode = "emailCode";
validateUser.mobileVerificationCode = "mobileCode";
Ran the application again, and this time the two hardcoded values were inserted as expected. Does this mean that that my two methods GenerateEmailCode() and GenerateMobileCode() are not working correctly?
Upvotes: 2
Views: 121
Reputation: 6638
there is probably something wrong with the way you are generating those random codes. As others have pointed out, the problem is most likely that you are creating instances of the random class too close in time.
try to declare a static random generator:
static Random r = new Random();
then use the static random to generate the code: (in what way you generate the code is up to you obviously but something like this should give you an idea):
public static string generateCode()
{
string chrs = "abcdefghijklmnopqrstuvwxyz";
char[] arr = chrs.ToCharArray();
string code = "";
for (int i = 0; i < 5; i++)
{
code += arr[r.Next(arr.Count())];
}
return code;
}
when i run this:
var firstCode = generateCode();
var secondCode = generateCode();
i get 2 different values.
Upvotes: 3