0

I need help creating a class file that randomly generates a number of 10 characters, 0-9, A-Z. By generating a PIN it will create an authorization workflow to allow the user to authenticate the app. This number will be directed to a config file instead of a URL. The user goes thru a auth process and instead of calling back a URL there will be a page to randonly generate a pin. The randomly generated PIN and access code will be stored in a database table.

This is what I have so far, will this code work if not what do I need to do to make it better. I also know that a RNGCryptoServiceProvider class is good for a strong random number generator but I'm not sure how to incorporate that into my code.

I also am using get set for the database fields. Would this be the best approach for REST API? I am not very familiar with API's. I would appreciate it any help on my code.

using System;

namespace AuthorizationPIN
{
using System;
using System.Text;


class RandomNumberSample
{
    static void Main(string[] args)
    {
        RandomGenerator generator = new RandomGenerator();
                    string str = generator.RandomString(10, false);
        Console.WriteLine($"Random string of 10 chars is {str}");



        Console.ReadKey();
    }
}
public class AuthorizationPIN
{
    public int Id { get; set; }
    public string AccessCode { get; set; }
    public string PIN { get; set; }

}
public class RandomGenerator
{
    // Generate a random number between two numbers  
    public int RandomNumber(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }

    // Generate a random string with a given size  
    public string RandomString(int size, bool upperCase)
    {
        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 (upperCase)
            return builder.ToString().ToUpper();
        return builder.ToString();
    }

    // Generate a random password  

}
mlynn
  • 59
  • 6
  • 2
    *"will this code work"* >> Well, have you tried it? Did it work? A quick look makes me think your `RandomGenerator` will probably cause some troubles since you create a new `Random` instance for each value you generate, which can be problematic (especially when called often / with high frequency). – bassfader Jul 17 '18 at 13:27
  • check : https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings-in-c – NoviceProgrammer Jul 17 '18 at 13:28
  • @sidprasher. On that link, that's it, that's all the code I need or is there more I need to add to it? – mlynn Jul 17 '18 at 13:33
  • Related to @bassfaders comment: [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Georg Patscheider Jul 17 '18 at 13:36
  • Do you want just uppercase A-Z? – Stevo Jul 18 '18 at 08:51

1 Answers1

0

There are plenty ways to do what you ask, one way, with a good distribution could be the following:

public string RandomString(int size, bool upperCase)
{
    using (var r = new RNGCryptoServiceProvider())
    {
        StringBuilder builder = new StringBuilder();
        var data = new byte[size];
        r.GetNonZeroBytes(data);

        var b = @"0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdifghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456";

        for (int i = 0; i < data.Length; i++)
        {
            var c = b[data[i]];
            builder.Append((upperCase) ? char.ToUpperInvariant(c) : c);
        }
        return builder.ToString();
    }
}
Yennefer
  • 5,704
  • 7
  • 31
  • 44