Create Random Character string in Asp.net
Create Random Character string in Asp.net
Use these Namespaces
using System.Security.Cryptography;
using System.Text;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(GetUniqueKey(15));
}
public static string GetUniqueKey(int maxSize)
{
char[] chars = new char[62];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
data = new byte[maxSize];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxSize);
foreach (byte b in data)
{
result.Append(chars[b %
(chars.Length)]);
}
return result.ToString();
}
The Out will be like :-
Output
:gxJQHtYKG6MhuYc3Ak2fZK3VOJ271w7Y
Comments
Post a Comment