如何在ASP.NET中对路径中的产品ID进行编码/解码

时间:2022-11-12 00:11:30

ASP.NET/Mono MVC4 shopping cart application users product codes as url names for awstats analytics like:

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

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

with default routing and controller

使用默认路由和控制器

    public class StoreController : MyApp.ControllerBase
    {

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

PRODUCT1 is product code. If product code contains / and other non-allowed characters in directory name, web server throws error.

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

Product code should be human readable since analytics shows urls. How to encode product code to passed as directory name and decode it back in controller ? Product code should remain unique after encoding.

产品代码应该是人类可读的,因为分析显示网址。如何将产品代码编码为目录名称并在控制器中解码?编码后,产品代码应保持唯一。

Is there some built-in function in ASP.NET or some source code which implements this ?

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

jquery, jquery ui, razor view engine is used.

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

Update

I tried recommendatiod in comment by creating Razor helper

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

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

and then using

然后使用

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

This causes error:

这会导致错误:

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

从客户端(%)检测到潜在危险的Request.Path值。 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?

url路径中不允许使用%chareter。所以UrlEncode无法使用。如何编码/解码?

1 个解决方案

#1


0  

You can use encryption and decryption algorithm (cryptography) to achieve your goal like this...

您可以使用加密和解密算法(加密)来实现这样的目标......

 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("\0".ToCharArray());
        }
    }

and use it like this...

并像这样使用它......

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

and in your action you can decrypt it as like this...

在你的行动中,你可以像这样解密它......

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

You can set your PasswordHash, SaltKey and VIKey i think this is what you want.

你可以设置你的PasswordHash,SaltKey和VIKey,我认为这就是你想要的。

You can check more about cryptography Reference

您可以查看有关加密参考的更多信息

#1


0  

You can use encryption and decryption algorithm (cryptography) to achieve your goal like this...

您可以使用加密和解密算法(加密)来实现这样的目标......

 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("\0".ToCharArray());
        }
    }

and use it like this...

并像这样使用它......

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

and in your action you can decrypt it as like this...

在你的行动中,你可以像这样解密它......

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

You can set your PasswordHash, SaltKey and VIKey i think this is what you want.

你可以设置你的PasswordHash,SaltKey和VIKey,我认为这就是你想要的。

You can check more about cryptography Reference

您可以查看有关加密参考的更多信息