samedi 26 décembre 2015

How to generate a random key in C# with stripes? [duplicate]

This question already has an answer here:

I would like to make a function that returns a key like this " XXXX-XXXX-XXXX-XXX" but when I use this code the key always return with the same 4 digit numbers. ex: ABCD-ABCD-ABCD-ABCD. Can anyone help?

public string Get(string Secret)
{
    string key = "" + RandomString(4) + "-" + RandomString(4) + "-" + RandomString(4) + "-" + RandomString(4);
    return key;
}

private string RandomString(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
          .Select(s => s[random.Next(s.Length)]).ToArray());
}




Aucun commentaire:

Enregistrer un commentaire