Token 生成简易代码

时间:2022-04-07 04:49:42

 

// like Ivj6eZRx40MTx2ZvnG8nA
public static string CreateToken()
{
    Guid g = Guid.NewGuid();
    var token = Convert.ToBase64String(g.ToByteArray()).Replace("=", "").Replace("+", "").Replace("/", "");
    return token;
}

// like Onh0h95n7nw
public static string CreateShortToken()
{
    var rnd = new Random();
    var tokenData = new byte[8];
    rnd.NextBytes(tokenData);
    var token = Convert.ToBase64String(tokenData).Replace("=", "").Replace("+", "").Replace("/", "");
    return token;
}