Creating a Random String for Authentication Token

There are times when we need to create our own authentication token so as to authenticate the request to our API project.
 
As per the recent requirement,  I have used to below mentioned code to generate an 8 character alphanumeric token to be saved in the db and the same would be used to each request made by that used to the API. 
  1. var allChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
  2. var random = new Random();  
  3. var resultToken = new string(  
  4.    Enumerable.Repeat(allChar , 8)  
  5.    .Select(token => token[random.Next(token.Length)]).ToArray());   
  6.    
  7.    string authToken = resultToken.ToString();  
Hope this helps.
 
Thanks
Vipul
Ebook Download
View all
Learn
View all