如何在 ASP.NET 中的路径中 encode/decode 产品 ID

How to encode/decode product id in path in ASP.NET

ASP.NET/Mono MVC4 购物车应用程序用户产品代码为 url awstats 分析的名称,如:

http://myshop.com/Store/Details/PRODUCT1

使用默认路由和控制器

    public class StoreController : MyApp.ControllerBase
    {

        public ActionResult Details(string id)
        {
  ....

PRODUCT1 是产品代码。 如果产品代码在目录名称中包含 / 和其他不允许的字符,Web 服务器将抛出错误。

产品代码应该是人类可读的,因为分析显示 urls。 如何编码产品代码以作为目录名称传递并将其解码回控制器? 编码后产品代码应保持唯一。

ASP.NET 中是否有一些 built-in 函数或一些实现此功能的源代码?

jquery, jquery ui, 使用了razor视图引擎。

更新

我尝试通过创建 Razor 助手在评论中推荐

@helper Details(string toode)
{
    @Url.Action("Details", new { id = HttpUtility.UrlEncode(toode) });
}

然后使用

<a href="@Details(someproduct)">

这会导致错误:

A potentially dangerous Request.Path value was detected from the client (%). GET /Store/Details/S%c3%9c%c3%9cTAJA HTTP/1.0

% chareter is not allowed in url paths. So UrlEncode cannot used. How to encode/decode?

你可以像这样使用加解密算法(cryptography)来达到你的目的...

 public class CryptoEngine
    {

        private static CryptoEngine _instance;
        private CryptoEngine() { }
        public static CryptoEngine Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CryptoEngine();

                return _instance;
            }
        }

        static readonly string PasswordHash = "@dM!nCo$";
        static readonly string SaltKey = "AdMinCos";
        static readonly string VIKey = "Polar!s@dM!nCoN$";

        public string Encrypt(string plainText)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

            byte[] cipherTextBytes;

            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    cipherTextBytes = memoryStream.ToArray();
                    cryptoStream.Close();
                }
                memoryStream.Close();
            }
            return Convert.ToBase64String(cipherTextBytes);
        }

        public string Decrypt(string encryptedText)
        {
            encryptedText = encryptedText.Trim();
            if (encryptedText.Contains(" "))
            {
                encryptedText = encryptedText.Replace(" ", "+");
            }

            byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

            var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
            var memoryStream = new MemoryStream(cipherTextBytes);
            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("[=10=]".ToCharArray());
        }
    }

并像这样使用它...

"/Store/Details?id="+CryptoEngine.Instance.Encrypt(toode))

并且在您的操作中,您可以像这样解密它...

public ActionResult Details(string id)
{
   string _id = CryptoEngine.Instance.Decrypt(id);
   .......

您可以设置您的PasswordHash、SaltKey和VIKey 我想这就是你想要的。

您可以查看有关密码学的更多信息 Reference