【问题标题】:safe and secure HTTPCookie storage in ASP.NET MVC C# applicationASP.NET MVC C# 应用程序中安全可靠的 HTTPCookie 存储
【发布时间】:2021-10-07 04:27:24
【问题描述】:

我正在使用下面的类来处理 cookie 并使用它们在我的 ASP.NET MVC 应用程序中存储/读取值(例如购物车项目等)

1.我想知道是否在浏览器中存储的值没有任何安全性并且任何人都可以查看其内容(使用以下实现)?我检查了值是否存储为一些十六进制值,但我怀疑此实现中是否存在任何特定的加密/安全性。

2.如何修改该类以将cookie值存储为加密信息?

using System;
using System.Web;

namespace My.Application.Sample
{
    public class CookieStore
    {


        public static void SetCookie(string key, string value)
        {
            SetCookie(key, value, TimeSpan.FromDays(14));
        }

        public static void SetCookie(string key, string value, TimeSpan expires)
        {
            string encodedValue = HttpUtility.UrlEncode(value);
            HttpCookie encodedCookie = new HttpCookie(key, encodedValue);

            if (HttpContext.Current.Request.Cookies[key] != null)
            {
                var cookieOld = HttpContext.Current.Request.Cookies[key];
                cookieOld.Expires = DateTime.Now.Add(expires);
                cookieOld.Value = encodedCookie.Value;
                HttpContext.Current.Response.Cookies.Add(cookieOld);
                
            }
            else
            {
                encodedCookie.Expires = DateTime.Now.Add(expires);
                HttpContext.Current.Response.Cookies.Add(encodedCookie);
            }
        }
        
        /// <summary>
        /// Return value stored in  a cookie by defined key, if not found returns empty string
        /// </summary>
        /// <param name="key"></param>
        /// <returns> never returns null! :) </returns>
        public static string GetCookie(string key)
        {
            string value = string.Empty;
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[key];

                //if (cookie != null)
                //{
                //    // For security purpose, we need to encrypt the value.
                //    HttpCookie decodedCookie = HttpSecureCookie.Decode(cookie);
                //    value = decodedCookie.Value;
                //}
                if (cookie != null)
                {
                    string encodedValue = cookie.Value;
                    value = HttpUtility.UrlDecode(encodedValue);
                }
            }
            catch (Exception)
            {

            }
            return value;
        }

    }
}

【问题讨论】:

    标签: c# asp.net-mvc security httpcookie


    【解决方案1】:

    您可以使用 ProtectUnprotect 方法来加密 cookie。请注意,两个字节具有相同的键值。使用 Protect 加密的数据只能使用 Unprotect 解密。

    加密方法

    public string encryptedCookie(string value)
    {
        var cookieText = Encoding.UTF8.GetBytes(value);
        var encryptedValue = Convert.ToBase64String(MachineKey.Protect(cookieText, "ProtectCookie"));
        return encryptedValue;
    }
          
    

    解密方法

    public string decryptedCookie(string value)
    {
        var bytes = Convert.FromBase64String(value);
        var output = MachineKey.Unprotect(bytes, "ProtectCookie");
        string result = Encoding.UTF8.GetString(output);
        return result;
    }
    

    您可以使用您的唯一密钥来代替"ProtectCookie"

    【讨论】:

    • 返回值在存入cookie之前是否需要进行URL编码?就像'stackoverflow.com/a/16618161/1080355'中描述的那样?
    • HttpServerUtility.UrlTokenEncode 使用加密生成输出字符串,但 UTF8.GetString 返回与加密字节等效的字符串。在实践中 HttpServerUtility.UrlTokenEncode 具有更高的安全性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多