0

I've read a nice guide a few days ago about generating a token on the server's side to have the time of the token's creation within the token, along with "Guid.NewGuid()" 's encryption.

However, I've tried to adjust the results to have a user's username within the token, rather than the date time. I'm close, but I cannot extract the username itself, I can only receive it with some random letters after it.

Code of the ASP.NET generic handler to GENERATE the token upon identification

ASCIIEncoding encoder = new ASCIIEncoding();
if(postType == "identify")
{
        byte[] binName = encoder.GetBytes(name);
        byte[] key = Guid.NewGuid().ToByteArray();
        string _token = Convert.ToBase64String(binName.Concat(key).ToArray());
        // The above creates a token with the name and the "key", works well
}

Code of the generic handler to decrypt the token (see example for result)

if(postType == "check")
{
       string _token = dict["token"] as string;
        byte[] data = Convert.FromBase64String(_token);
        string theCode = encoder.GetString(data); // This will get both the username and the GUID key within
        context.Response.Write(jss.Serialize(new Code { eCode = theCode })); // Returns in JSON, irrelevant to the question, it works well
}

EXAMPLE: If the name would be "user", then the varialbe "theCode" would hold the value of "userXyZxYzXyZ" (while XyZ stands for the GUID's "random" key).

I think it is fair to say that my question is how to separate this GUID's key from the username upon decryption

Kfir Eichenblat
  • 449
  • 2
  • 8
  • 27

1 Answers1

2

A guid is 38 characters long, so the name will be theCode.SubString(0, theCode.Length - 38). Alternately, you can compare the current user's name with theCode: theCode.StartsWith(name).

bmm6o
  • 6,187
  • 3
  • 28
  • 55