142 lines
4.7 KiB
C#
142 lines
4.7 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Nirvana.Common
|
|
{
|
|
public class WebHelper
|
|
{
|
|
#region cookie操作
|
|
/// <summary>
|
|
/// 写cookie值
|
|
/// </summary>
|
|
/// <param name="strName">名称</param>
|
|
/// <param name="strValue">值</param>
|
|
/// <param name="domain">域名</param>
|
|
public static void WriteCookie(string strName, string strValue, string domain = "")
|
|
{
|
|
if (!string.IsNullOrEmpty(domain))
|
|
{
|
|
MyHttpContext.current.Response.Cookies.Append(strName, strValue, new Microsoft.AspNetCore.Http.CookieOptions
|
|
{
|
|
Domain = domain
|
|
});
|
|
}
|
|
else
|
|
{
|
|
MyHttpContext.current.Response.Cookies.Append(strName, strValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写cookie值
|
|
/// </summary>
|
|
/// <param name="strName">名称</param>
|
|
/// <param name="strValue">值</param>
|
|
/// <param name="strValue">过期时间(分钟)</param>
|
|
/// <param name="domain">域名</param>
|
|
public static void WriteCookie(string strName, string strValue, int expires, string domain = "")
|
|
{
|
|
if (!string.IsNullOrEmpty(domain))
|
|
{
|
|
MyHttpContext.current.Response.Cookies.Append(strName, strValue, new Microsoft.AspNetCore.Http.CookieOptions
|
|
{
|
|
Domain = domain,
|
|
Expires = DateTime.Now.AddMinutes(expires)
|
|
});
|
|
}
|
|
else
|
|
{
|
|
MyHttpContext.current.Response.Cookies.Append(strName, strValue, new Microsoft.AspNetCore.Http.CookieOptions
|
|
{
|
|
Expires = DateTime.Now.AddMinutes(expires)
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读cookie值
|
|
/// </summary>
|
|
/// <param name="strName">名称</param>
|
|
/// <returns></returns>
|
|
public static string GetCookie(string strName)
|
|
{
|
|
if (MyHttpContext.current !=null && MyHttpContext.current.Request.Cookies != null && !string.IsNullOrEmpty(MyHttpContext.current.Request.Cookies[strName]))
|
|
{
|
|
return MyHttpContext.current.Request.Cookies[strName];
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除Cookie对象
|
|
/// </summary>
|
|
/// <param name="CookiesName">Cookie对象名称</param>
|
|
public static void RemoveCookie(string CookiesName)
|
|
{
|
|
if (MyHttpContext.current != null && MyHttpContext.current.Request.Cookies != null && !string.IsNullOrEmpty(MyHttpContext.current.Request.Cookies[CookiesName]))
|
|
{
|
|
MyHttpContext.current.Response.Cookies.Delete(CookiesName);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Session操作
|
|
/// <summary>
|
|
/// 写Session
|
|
/// </summary>
|
|
/// <typeparam name="T">Session键值的类型</typeparam>
|
|
/// <param name="key">Session的键名</param>
|
|
/// <param name="value">Session的键值</param>
|
|
public static void WriteSession(string key, byte[] value)
|
|
{
|
|
if (key.IsEmpty())
|
|
return;
|
|
MyHttpContext.current.Session.Set(key, value);
|
|
}
|
|
/// <summary>
|
|
/// 写Session
|
|
/// </summary>
|
|
/// <param name="key">Session的键名</param>
|
|
/// <param name="value">Session的键值</param>
|
|
public static void WriteSession(string key, string value)
|
|
{
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
byte[] bt = Encoding.Default.GetBytes(value);
|
|
WriteSession(key, bt);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读取Session的值
|
|
/// </summary>
|
|
/// <param name="key">Session的键名</param>
|
|
/// <returns></returns>
|
|
public static string GetSession(string key)
|
|
{
|
|
if (key.IsEmpty())
|
|
return string.Empty;
|
|
if (MyHttpContext.current.Session == null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
var value = MyHttpContext.current.Session.GetString(key);
|
|
return value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定Session
|
|
/// </summary>
|
|
/// <param name="key">Session的键名</param>
|
|
public static void RemoveSession(string key)
|
|
{
|
|
if (key.IsEmpty())
|
|
return;
|
|
MyHttpContext.current.Session.Remove(key);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|