locked
Create Authorization class that automatically generates a PIN number RRS feed

  • Question

  • User-501297529 posted

    I'm not sure if I'm posted this in the right section but I need assistance on how to create an AuthorizationPIN class that automatically generates a random PIN number when a user logs in. Also how to create a AuthorizationPINRepository as well. Are these one in the same? How would I create something like this?

    Saturday, July 14, 2018 11:00 PM

Answers

  • User1724605321 posted

    Hi bootzilla,

    Not sure what's your current problem . I only provide codes sample about how to create random number generator .The codes will be :

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

    When you want to create the number , you can use like :

    var pin= RandomString(10);

    Then you could save the pin number string to database.  Maybe please explain and help us understand more about your problem . 

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 19, 2018 5:56 AM

All replies

  • User409696431 posted

    Can you elaborate on what you are trying to do?  What is the PIN number for?  What format is it?  What is a PIN repository?

    You can certainly create a random number, for example, any time someone logs in, and store that number in a database - but, why?

    Sunday, July 15, 2018 12:57 AM
  • User-501297529 posted

    The way the app is currently constructed is when a user logs in and it calls back a URL. By generating a PIN it will create an authorization workflow to allow the user to authenticate the app. It will be a 10-12 pin that the user enters into a config file instead of that 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 new database table. Hopefully this makes sense.

    Sunday, July 15, 2018 8:12 PM
  • User1724605321 posted

    Hi bootzilla,

    What is your PIN format . You can use GUID or create random digits :

    Random rand = new Random(100);
    int code =  rand.Next(000000000, 999999999);

    https://www.c-sharpcorner.com/article/generating-random-number-and-string-in-C-Sharp/ 

    Best Regards,

    Nan Yu

    Monday, July 16, 2018 5:43 AM
  • User-501297529 posted

    Hi Nan,

    10 characters 0-9, A-Z uppercase. When doing a database lookup, the database will find a match if the client submits lower case letters 

    So that code example and the link to the randomly generated number, could that be used to do what I need to do?

    Here is what I have so far using the code example  you provided. I have no idea if I am anywhere close. I created a REST API using the get set for each column in the table.

    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  
          
        }
    }
    

    Thanks

    Monday, July 16, 2018 2:38 PM
  • User1724605321 posted

    Hi bootzilla ,

    10 characters 0-9, A-Z uppercase. When doing a database lookup, the database will find a match if the client submits lower case letters 

    OK , then you could use linQ to create that :

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

    But it is recommended to use the RNGCryptoServiceProvider class if you need a strong random number generator :

    https://stackoverflow.com/a/1344255/5751404 

    Best Regards,

    Nan Yu
     

    Tuesday, July 17, 2018 5:22 AM
  • User-501297529 posted

    Hi Nan,

    I don't think I'll need the RNGCryptoServiceProvider class. The code you provided should do. A few questions. How do this code look? Do I need to add or remove to my code? Will this work for 10 character limit?

    I also need to enter this number to a config file, how do I do that?

    I know these are a lot of questions but I've not done this before.

    using System;
    using System.Linq;
    using System.Text;
    
    namespace AuthorizationPIN
    {
    
    
    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();
    }
    private static Random random = new Random();
    public static string RandomString(int length)
    {
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length)
    .Select(s => s[random.Next(s.Length)]).ToArray());
    }
    
    }
    }

     

    Also what about the database columns. It needs get/set properties for each column in that table

    public class AuthorizationPIN
        {
            public int Id { get; set; }
            public string AccessCode { get; set; }
            public string PIN { get; set; }
    
        }
    



    Is this good code to use for that?

    Tuesday, July 17, 2018 1:37 PM
  • User1724605321 posted

    Hi bootzilla,

    How do this code look? Do I need to add or remove to my code?

    Have you checked your codes ? Any error shows or get the random string ?

    Will this work for 10 character limit?

    Yes , set the length of RandomString function .

    I also need to enter this number to a config file, how do I do that?

    File.AppendAllText appends a string to a file , see code sample :

    https://www.dotnetperls.com/file-appendalltext 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file 

    Best Regards,

    Nan Yu

    Wednesday, July 18, 2018 2:48 AM
  • User-501297529 posted

    Nan Yu

    Yes , set the length of RandomString function .

    How do I do that in the RandomString function can you give specifics on what to put in that function for the length of 10

    Is that like this:

    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();
    }
    private static Random random = new Random();
    public static string RandomString(int length)
    {
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return new string(Enumerable.Repeat(chars, length, 10)
    .Select(s => s[random.Next(s.Length)]).ToArray());
    }
    

    I also asked about the database columns, these are the columns in the database:

    Id (INT, Identity, PK), Pin (VARCHAR(10)), AccessCode (VARCHAR(1000)), Callback (VARCHAR(2083)), and ExpirationDate (DATETIME2(3))

    Is this right:

    public class AuthorizationPIN
        {
            public int Id { get; set; }
            public string AccessCode { get; set; }
            public string PIN { get; set; }
    public string Callback { get; set; }
           public string ExpirationDate { get; set; }
    }
    Wednesday, July 18, 2018 2:19 PM
  • User1724605321 posted

    Hi bootzilla,

    Not sure what's your current problem . I only provide codes sample about how to create random number generator .The codes will be :

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

    When you want to create the number , you can use like :

    var pin= RandomString(10);

    Then you could save the pin number string to database.  Maybe please explain and help us understand more about your problem . 

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 19, 2018 5:56 AM