简体   繁体   English

如何从Base64String中提取特定数据?

[英]How to pull specific data from a Base64String?

This is directly related to how to generate a unique token which expires after 24 hours? 这与如何生成在24小时后失效的唯一令牌直接相关吗?

What I am attempting to do is embed the following: 我正在尝试做的是嵌入以下内容:

  1. A Page Number (0 through 6) 页码(0到6)
  2. Current Date/Time Stamp in UTC Format UTC格式的当前日期/时间戳
  3. and A Unique GUID 和唯一的GUID

The code I have so far is this: 到目前为止,我的代码是:

private string GenerateToken(Int32 pageNumber)
{
    byte[] currentTimeStamp = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
    byte[] key = Guid.NewGuid().ToByteArray();
    byte[] newPageNumber = BitConverter.GetBytes(pageNumber);
    string token = Convert.ToBase64String(newPageNumber.Concat(currentTimeStamp).Concat(key).ToArray());
    return token;
}

private tokenClass TokenAuthenticates(string token)
{
    byte[] data = Convert.FromBase64String(token);

    tokenClass _token = new tokenClass()
    {
        PageNumber = 0,
        TokenDateTimeStamp = DateTime.FromBinary(BitConverter.ToInt64(data, 1)),
        TokenKey = new Guid(),
        Validates = (DateTime.FromBinary(BitConverter.ToInt64(data, 1)) < DateTime.UtcNow.AddHours(-2))
    };

    return _token;
}

The page and Guid parameters in the decoder are not yet figured out yet so they are basically dummies. 解码器中的page和Guid参数尚未确定,因此它们基本上是虚拟的。

What do I need to do to make this work? 我需要做些什么才能使这项工作?

Generate your token like this: 像这样生成令牌:

private static string GenerateToken(Int32 pageNumber)
{
    byte[] currentTimeStamp = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
    var keyGuid = Guid.NewGuid();
    byte[] key = keyGuid.ToByteArray();
    byte[] newPageNumber = BitConverter.GetBytes(pageNumber);

    // date plus page number plus key
    string token = Convert.ToBase64String(currentTimeStamp.Concat(newPageNumber).Concat(key).ToArray());
    return token;
}

Read the token like this (in your TokenAuthenticates method): 像这样读取令牌(在TokenAuthenticates方法中):

byte[] data = Convert.FromBase64String(token);

// It will take eight bytes starting at index 0
DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0)); 

// 8 first bytes was taken by date so lets skip 8 and then take 4 since page number is an integer and takes 4 bytes
int pageNumber = BitConverter.ToInt32(data.Skip(8).Take(4).ToArray(), 0);

// 8 bytes for date + 4 bytes for page number so we skip 12 and then take 16 for Guid
// Guid can be generated directly from the bytes
Guid key = new Guid(data.Skip(12).Take(16).ToArray());

Here is a way so you do not have to hardcode the numbers or determine the size. 这样您就不必对数字进行硬编码或确定大小。 Use the sizeof operator to do the determination for you: 使用sizeof运算符可以为您确定:

int pageNumber = BitConverter.ToInt32(data.Skip(sizeof(long))
                     .Take(sizeof(int)).ToArray(), 0);

// Skip date and pageNumber, the rest is Guid
Guid key = new Guid(data.Skip(sizeof(long) + sizeof(int)).ToArray());

I would call the method AuthenticateToken since it is an action verb and sounds more readable and clear. 我将其称为AuthenticateToken方法,因为它是一个动作动词,听起来更易读和清晰。 You can perform your further validation after you have read the token. 阅读令牌后,您可以执行进一步的验证。 You may want to consider encrypting the token too. 您可能还需要考虑加密令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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