asp.net web api编码url的一部分 [英] asp.net web api encoding the part of url

查看:65
本文介绍了asp.net web api编码url的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i想要这样假设我有网址http:// localhost / myApp / api / Password / 435





i想要ecode 435到AL2uJOIm1CI =或者是一些无法理解的东西,比如http:// localhost / myApp / api / Password / AL2uJOIm1CI =



以及如何解码AL2uJOIm1CI =再次435

Hi All,

i want like this suppose i have url http://localhost/myApp/api/Password/435


i want ecode 435 to AL2uJOIm1CI= or somthing that can be not understandable , like http://localhost/myApp/api/Password/AL2uJOIm1CI=

and how to decode AL2uJOIm1CI= to again 435

推荐答案

您可以在将值传递给url然后解密之前对其进行加密它在你的页面中。

这是一个加密和解密值的例子:



加密值:

You can encrypt your value before passing it to url and then decrypt it in your page.
Here is an example of encrypting and decrypting value :

encrypting value :
public static string Encrypt_QueryString(string str)
   {
       string EncrptKey = "2013;[pnuLIT)WebCodeExpert";
       byte[] byKey = { };
       byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
       byKey = System.Text.Encoding.UTF8.GetBytes(EncrptKey.Substring(0, 8));
       DESCryptoServiceProvider des = new DESCryptoServiceProvider();
       byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
       MemoryStream ms = new MemoryStream();
       CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
       cs.Write(inputByteArray, 0, inputByteArray.Length);
       cs.FlushFinalBlock();
       return Convert.ToBase64String(ms.ToArray());
   }





解密价值:





Decrypting value :

public static string Decrypt_QueryString(string str)
    {
        string strreturn = string.Empty;
        try
        {
            str = str.Replace(" ", "+");
            string DecryptKey = "2013;[pnuLIT)WebCodeExpert";
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byte[] inputByteArray = new byte[str.Length];

            byKey = System.Text.Encoding.UTF8.GetBytes(DecryptKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(str);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            strreturn = encoding.GetString(ms.ToArray());

        }
        catch (Exception ex)
        {
            //throw ex;
            ex.Message.ToString();
        }
        return strreturn;
    }





不要忘记包含以下名称空间:



Do not forget to include following name space :

using System.Security.Cryptography;





祝你好运



Good luck


试试这些链接 -

DotNetTrace.Net:MVC 4中的加密和解密URL [ ^ ]

用于加密查询字符串的ASP.NET MVC值提供程序dotnetExpertGuide.com [ ^ ]
Try these links -
DotNetTrace.Net: Encrypt and Decrypt URL in MVC 4[^]
ASP.NET MVC Value Provider for encrypted query string | dotnetExpertGuide.com[^]


这篇关于asp.net web api编码url的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆