gpt4 book ai didi

asp.net-mvc - 在 ASP.NET MVC 中加密 URL 中的 id

转载 作者:行者123 更新时间:2023-12-02 01:11:43 26 4
gpt4 key购买 nike

我正在尝试对 URL 中的加密 ID 进行编码。像这样: http://www.calemadr.com/Membership/Welcome/9xCnCLIwzxzBuPEjqJFxC6XJdAZqQsIDqNrRUJoW6229IIeeL4eXl5n1cnYapg+N

但是,它要么无法正确编码,并且在加密中出现斜杠“/”,要么收到来自 IIS 的错误:请求过滤模块配置为拒绝包含双转义序列的请求。

我尝试了不同的编码,但都失败了:

  • HttpUtility.HtmlEncode
  • HttpUtility.UrlEncode
  • HttpUtility.UrlPathEncode
  • HttpUtility.UrlEncodeUnicode

更新

问题是当我加密 Guid 并将其转换为 base64 字符串时,它会包含不安全的 url 字符。当然,当我尝试导航到包含不安全字符的 url 时,IIS(7.5/windows 7)会崩溃。对 Base64 加密字符串进行 Url 编码会在 IIS 中引发错误(请求过滤模块配置为拒绝包含双转义序列的请求。)。我不确定它如何检测双编码字符串,但它确实检测到了。

尝试以上方法对base64加密字符串进行编码后。我决定删除base64编码。然而,这会将加密文本保留为 byte[]。我尝试对 byte[] 进行 UrlEncoding,它是卡在 httpUtility.Encode 方法上的重载之一。同样,虽然它是 URL 编码的,但 IIS 不喜欢它,并提供了一个“找不到页面”。

在网上挖掘后,我发现了一个 HexEncoding/Decoding类(class)。将十六进制编码应用于加密字节就可以了。输出是 url 安全的。另一方面,我在解码和解密十六进制字符串方面没有遇到任何问题。

最佳答案

我写了一篇简短的博客post关于这个主题,包括完整的源代码。

它使您能够使用 16 个字符 key 加密和解密以查询字符串形式存储的数据:

I found a great set of base classes to solve this but for the most part it comes down to one class. This class requires a 16 char key of some kind to do the encryption and a value to encrypt. You can also set an expiration value if needed.

using System.Collections.Specialized;
using System.Security;
using System.Text;
using System.Web;
using EncryptionMVC.Security.Encryption.Utility.Interfaces;
using EncryptionMVC.Security.Encryption.Utility;
namespace Security.Encryption.QueryString
{
///
/// Provides a secure means for transfering data within a query string.
///
public class SecureQueryString : NameValueCollection
{

private string timeStampKey = '__TS__';
private string dateFormat = 'G';
private IEncryptionUtility mEncryptionUtil;
private DateTime m_expireTime = DateTime.MaxValue;

///
/// Creates an instance with a specified key.
///
/// The key used for cryptographic functions, required 16 chars in length.
public SecureQueryString(string key) : base()
{
mEncryptionUtil = new EncryptionUtility(key);
}

///
/// Creates an instance with a specified key and an encrypted query string.
///
/// The key used for cryptographic functions, required 16 chars in length.
/// An encrypted query string generated by a instance.
public SecureQueryString(string key, string queryString) : this(key)
{
Deserialize(DecryptAndVerify(queryString));
CheckExpiration();
}

///
/// Returns a encrypted query string.
///
///
public override string ToString()
{
return EncryptAndSign(Serialize());
}

private void Deserialize(string queryString)
{
string[] nameValuePairs = queryString.Split('&');
for (int i = 0; i <= nameValuePairs.Length - 1; i++) {
string[] nameValue = nameValuePairs(i).Split('=');
if (nameValue.Length == 2) {
base.Add(nameValue(0), nameValue(1));
}
}

if (base.GetValues(timeStampKey) != null) {
string[] strExpireTime = base.GetValues(timeStampKey);
m_expireTime = Convert.ToDateTime(strExpireTime(0));
}
}

private string Serialize()
{
StringBuilder sb = new StringBuilder();
foreach (string key in base.AllKeys) {
sb.Append(key);
sb.Append('=');
sb.Append(base.GetValues(key)(0).ToString());
sb.Append('&');
}

sb.Append(timeStampKey);
sb.Append('=');
sb.Append(m_expireTime.ToString(dateFormat));

return sb.ToString();
}

private string DecryptAndVerify(string input)
{
return mEncryptionUtil.Decrypt(input);
}

private string EncryptAndSign(string input)
{
return mEncryptionUtil.Encrypt(input);
}

private void CheckExpiration()
{
if (DateTime.Compare(m_expireTime, DateTime.Now) < 0) {
throw new ExpiredQueryStringException();
}
}

///
/// Gets or sets the timestamp in which this string should expire
///
public DateTime ExpireTime {
get { return m_expireTime; }
set { m_expireTime = value; }
}
}
}

To encrypt some value and pass it to another action in MVC you would do something like the below.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
SecureQueryString qs = new SecureQueryString(mKey);

qs('YourName') = collection('name');
qs.ExpireTime = DateTime.Now.AddMinutes(2);

Response.Redirect('Home.aspx/About?data=' + HttpUtility.UrlEncode(qs.ToString()));
}

In the action that we redirect to, you would need to have this same key and the query string value itself to decrypt it. Keep in mind that if you don't have the correct key or if you try to decrypt the value after the expiration, the class will throw an exception.

public ActionResult About()
{
if (Request('data') != null) {
try {
SecureQueryString qs = new SecureQueryString(mKey, Request('data'));

ViewData('Message') = 'Your name is ' + qs('YourName');
}
catch (Exception ex) {

}
}
return View();
}

I didn't spend much time explaining the source in depth because it has been so long since I wrote it. Also keep in mind this was long before my test first days ... (but it does appear to work)

As always, the source code for this sample is available for download.

关于asp.net-mvc - 在 ASP.NET MVC 中加密 URL 中的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com