mardi 17 juillet 2018

How to randomly generate a number or characters in an Authorization Class file

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  

}




Aucun commentaire:

Enregistrer un commentaire