Files
2026-08-21 14:15:16 +08:00

157 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WxOrder.Framework.Helpers;
/// <summary>
/// Cookie对象模型
/// </summary>
public class CookieItem
{
public string Name { get; set; }
public string Value { get; set; }
public string Domain { get; set; }
public string Path { get; set; }
public bool Secure { get; set; }
public bool HttpOnly { get; set; }
public DateTime? Expires { get; set; }
public DateTime? Creation { get; set; }
public DateTime? LastAccess { get; set; }
public int SameSite { get; set; }
public int Priority { get; set; }
}
/// <summary>
/// Cookie转换帮助类
/// </summary>
public static class CookieConvertHelper
{
/// <summary>
/// 将CookieItem列表转换为Cookie字符串
/// </summary>
/// <param name="cookieItems">Cookie对象列表</param>
/// <returns>Cookie字符串,格式:name1=value1; name2=value2</returns>
public static string ToCookieString(List<CookieItem> cookieItems)
{
if (cookieItems == null || cookieItems.Count == 0)
return string.Empty;
var builder = new StringBuilder();
for (int i = 0; i < cookieItems.Count; i++)
{
var item = cookieItems[i];
if (!string.IsNullOrEmpty(item.Name) && item.Value != null)
{
builder.Append($"{item.Name}={item.Value}");
if (i < cookieItems.Count - 1)
builder.Append("; ");
}
}
return builder.ToString();
}
/// <summary>
/// 将单个CookieItem转换为Cookie字符串
/// </summary>
/// <param name="cookieItem">Cookie对象</param>
/// <returns>Cookie字符串,格式:name=value</returns>
public static string ToCookieString(CookieItem cookieItem)
{
if (cookieItem == null || string.IsNullOrEmpty(cookieItem.Name))
return string.Empty;
return $"{cookieItem.Name}={cookieItem.Value}";
}
/// <summary>
/// 将CookieItem列表转换为字典
/// </summary>
/// <param name="cookieItems">Cookie对象列表</param>
/// <returns>Cookie字典</returns>
public static Dictionary<string, string> ToDictionary(List<CookieItem> cookieItems)
{
var dict = new Dictionary<string, string>();
if (cookieItems == null || cookieItems.Count == 0)
return dict;
foreach (var item in cookieItems)
{
if (!string.IsNullOrEmpty(item.Name) && !dict.ContainsKey(item.Name))
{
dict.Add(item.Name, item.Value);
}
}
return dict;
}
/// <summary>
/// 将CookieItem列表转换为System.Net.CookieCollection
/// </summary>
/// <param name="cookieItems">Cookie对象列表</param>
/// <param name="domain">域名</param>
/// <returns>CookieCollection</returns>
public static System.Net.CookieCollection ToCookieCollection(List<CookieItem> cookieItems, string domain = "mobile.yangkeduo.com")
{
var cookieCollection = new System.Net.CookieCollection();
if (cookieItems == null || cookieItems.Count == 0)
return cookieCollection;
foreach (var item in cookieItems)
{
if (string.IsNullOrEmpty(item.Name))
continue;
var cookie = new System.Net.Cookie
{
Name = item.Name,
Value = item.Value ?? string.Empty,
Domain = string.IsNullOrEmpty(item.Domain) ? domain : item.Domain,
Path = string.IsNullOrEmpty(item.Path) ? "/" : item.Path,
Secure = item.Secure,
HttpOnly = item.HttpOnly,
Expires = item.Expires ?? DateTime.MinValue
};
cookieCollection.Add(cookie);
}
return cookieCollection;
}
/// <summary>
/// 从JSON数组字符串转换为Cookie字符串
/// </summary>
/// <param name="jsonArray">JSON数组字符串</param>
/// <returns>Cookie字符串</returns>
public static string FromJsonToCookieString(string cookie)
{
try
{
var cookieItems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CookieItem>>(cookie);
return ToCookieString(cookieItems);
}
catch
{
return string.Empty;
}
}
/// <summary>
/// 从JSON数组字符串转换为字典
/// </summary>
/// <param name="jsonArray">JSON数组字符串</param>
/// <returns>Cookie字典</returns>
public static Dictionary<string, string> FromJsonToDictionary(string jsonArray)
{
try
{
var cookieItems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CookieItem>>(jsonArray);
return ToDictionary(cookieItems);
}
catch
{
return new Dictionary<string, string>();
}
}
}