简体   繁体   中英

how to prevent special characters at generating a token in c#

i generate a token which converts the datetime in bytes. Here is how i generate the token:

public string generateToken()
    {
        byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
        byte[] key = new Guid().ToByteArray();
        string token = Convert.ToBase64String(time.Concat(key).ToArray());
        return token;
    }

This is how it looks like when the token is generated:

chas42Sbo9AAAAAAAAAAAAAAAAAAAAA

but on some days it generates special characters,too. like in this example:

chs2BiT/z0gAAAAAAAAAAAAAAAAAAAAA

i parse the result in a link to redirect to another page

http://test.com/abo.aspx?chas42Sbo9AAAAAAAAAAAAAAAAAAAAA

with the special characters it looks like this:

http://test.com/abo.aspx?chs2BiT/z0gAAAAAAAAAAAAAAAAAAAAA

and this doesnt work.

is it possible to generate a token but without special characters?

This is how Base64 is defined:

The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.

So you can't use Convert.ToBase64String if you don't want / character to appear in your token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM