问题描述
我正在将一个面向 net472
的项目移植到 netstandard
.我遇到的最后一个 System.Web
依赖项是 HttpServerUtility.UrlTokenEncode(Byte[])
.
I'm porting a project targeting net472
to netstandard
. The last System.Web
dependency I'm stuck with is HttpServerUtility.UrlTokenEncode(Byte[])
.
我找到了 Microsoft.AspNetCore.WebUtilities
,其中包含 Base64UrlTextEncoder
和 WebEncoders
,但这些不能与 UrlTokenEncode
/Decode
互换,因为它附加/期望 =
在末尾填充字符,例如:
I found Microsoft.AspNetCore.WebUtilities
, which contains Base64UrlTextEncoder
and WebEncoders
, but those are not interchangeable with the UrlTokenEncode
/Decode
, as it appends / expects the number of =
padding characters at the end, e.g.:
var data = Encoding.UTF8.GetBytes("SO");
Convert.ToBase64String(data); // U08=
HttpServerUtility.UrlTokenEncode(data); // U081 - this is what's expected and
// the only thing UrlTokenDecode can handle
Base64UrlTextEncoder.Encode(data); // U08
WebEncoders.Base64UrlEncode(data); // U08
据我所知,没有其他区别(我使用随机字符串进行测试),但它还引入了其他一些依赖项(Microsoft.Net.Http.Headers & Microsoft.Extensions.Primitives),即我真的不需要那个项目.
As far as I can tell, there are no other differences (I ran tests with random strings), but it also pulls in some other dependencies (Microsoft.Net.Http.Headers & Microsoft.Extensions.Primitives), that I don't really need in that project.
是否有任何可以直接替换的 nuget 包?如果没有,我正在考虑自己实施.
Is there any nuget package with a drop-in replacement? I'm thinking of implementing this myself, if not.
推荐答案
对于那些寻找这个已删除的实用程序方法的答案并希望迁移遗留应用程序的人,我已经从 M$ 源代码中提取了一些内容.
For those whom looks for the answer of this removed utility method and wish to migrate legacy application across, I have done some extract from M$ source.
private static string UrlTokenEncode(byte[] input)
{
if (input == null)
throw new ArgumentNullException("input");
if (input.Length < 1)
return String.Empty;
char[] base64Chars = null;
////////////////////////////////////////////////////////
// Step 1: Do a Base64 encoding
string base64Str = Convert.ToBase64String(input);
if (base64Str == null)
return null;
int endPos;
////////////////////////////////////////////////////////
// Step 2: Find how many padding chars are present in the end
for (endPos = base64Str.Length; endPos > 0; endPos--)
{
if (base64Str[endPos - 1] != '=') // Found a non-padding char!
{
break; // Stop here
}
}
////////////////////////////////////////////////////////
// Step 3: Create char array to store all non-padding chars,
// plus a char to indicate how many padding chars are needed
base64Chars = new char[endPos + 1];
base64Chars[endPos] = (char)((int)'0' + base64Str.Length - endPos); // Store a char at the end, to indicate how many padding chars are needed
////////////////////////////////////////////////////////
// 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;
}
}
return new string(base64Chars);
}
private static byte[] UrlTokenDecode(string input)
{
if (input == null)
throw new ArgumentNullException("input");
int len = input.Length;
if (len < 1)
return new byte[0];
///////////////////////////////////////////////////////////////////
// Step 1: Calculate the number of padding chars to append to this string.
// The number of padding chars to append is stored in the last char of the string.
int numPadChars = (int)input[len - 1] - (int)'0';
if (numPadChars < 0 || numPadChars > 10)
return null;
///////////////////////////////////////////////////////////////////
// Step 2: Create array to store the chars (not including the last char)
// and the padding chars
char[] base64Chars = new char[len - 1 + numPadChars];
////////////////////////////////////////////////////////
// Step 3: Copy in the chars. Transform the "-" to "+", and "*" to "/"
for (int iter = 0; iter < len - 1; iter++)
{
char c = input[iter];
switch (c)
{
case '-':
base64Chars[iter] = '+';
break;
case '_':
base64Chars[iter] = '/';
break;
default:
base64Chars[iter] = c;
break;
}
}
////////////////////////////////////////////////////////
// Step 4: Add padding chars
for (int iter = len - 1; iter < base64Chars.Length; iter++)
{
base64Chars[iter] = '=';
}
// Do the actual conversion
return Convert.FromBase64CharArray(base64Chars, 0, base64Chars.Length);
}
这篇关于HttpServerUtility.UrlTokenEncode 替换 netstandard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!