2016-11-30 65 views

回答

3

當你偷看類MachineKey45CryptoSystem你會發現,用來序列防僞標記Protect方法:

public string Protect(byte[] data) 
{ 
    return HttpServerUtility.UrlTokenEncode(MachineKey.Protect(data, MachineKey45CryptoSystem._purposes)); 
} 

當你去更深,你會在HttpEncoder找到方法:

internal string UrlTokenEncode(byte[] input) { 
.... 
} 

內它的身體:

base64Str = Convert.ToBase64String(input); 

an d

// Step 3: Copy in the other chars. Transform the "+" to "-", and "/" to "_" 
for (int iter = 0; iter < endPos; iter++) { 
    char c = base64Str[iter]; 

    switch (c) { 
     case '+': 
      base64Chars[iter] = '-'; 
      break; 

     case '/': 
      base64Chars[iter] = '_'; 
      break; 

     case '=': 
      Debug.Assert(false); 
      base64Chars[iter] = c; 
      break; 

     default: 
      base64Chars[iter] = c; 
      break; 
    } 
} 

來概括 - 有效字符是字母,數字,-_

+0

感謝您的回覆。我們怎樣才能確保令牌中不會有'!','〜','*','#'等字符?因爲這不是在is代碼中處理的。 –

+0

Convert.ToBase64String確保。請參閱64位編碼字符串的規格。 – pwas