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.
- var allChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var random = new Random();
- var resultToken = new string(
- Enumerable.Repeat(allChar , 8)
- .Select(token => token[random.Next(token.Length)]).ToArray());
-
- string authToken = resultToken.ToString();
Hope this helps.
Thanks
Vipul