public class HtmlContextSheep
{
#region 获取 From 和 Query 的数据
/// <summary>
///获取POST 表单提交的值
/// </summary>
/// <param name="key">key</param>
/// <param name="isSafe">是否进行安全检查</param>
/// <returns>返回From表单值</returns>
public static string Form(Microsoft.AspNetCore.Http.HttpContext context, string key, bool isSafe = true)
{
string result = "";
if (!string.IsNullOrWhiteSpace(context.Request.Form[key]))
{
result = context.Request.Form[key];
if (isSafe)
{
if (Common.SafeCommon.IsSafeString(result))
{
return result;
}
}
}
return result;
}
/// <summary>
/// 获取GET 请求的值
/// </summary>
/// <param name="key">key</param>
/// <param name="isSafe">是否进行安全检查</param>
/// <returns></returns>
public static string Query(Microsoft.AspNetCore.Http.HttpContext context, string key, bool isSafe = true)
{
string result = "";
if (!string.IsNullOrWhiteSpace(context.Request.Query[key]))
{
result = context.Request.Query[key];
if (isSafe)
{
if (Common.SafeCommon.IsSafeString(result))
{
return result;
}
}
}
return result;
}
#endregion
#region session 的操作
#region 设置session
/// <summary>
/// 设置session
/// </summary>
/// <param name="key">session的键</param>
/// <param name="value">session的值</param>
/// Microsoft.AspNetCore.Http.HttpContext context,
public static void SetSession(Microsoft.AspNetCore.Http.HttpContext context,string key, string value)
{
if (String.IsNullOrWhiteSpace(context.Session.GetString(key)))
{
context.Session.Remove(key);
}
context.Session.SetString(key, value);
}
#endregion
#region 获取Session
/// <summary>
/// 获取Session
/// </summary>
/// <param name="key">session的键</param>
/// <returns>返回结果的字符串</returns>
public static String GetSession(Microsoft.AspNetCore.Http.HttpContext context, string key)
{
string result = "";
result = context.Session.GetString(key);
return result;
}
#endregion
#endregion
#region cookie的操作
#region 为cookie赋值 public static void SetCookie(string key, String value , int count=1)
/// <summary>
/// 为cookie赋值
/// </summary>
/// <param name="key">cookie的名字</param>
/// <param name="value">值</param>
/// <param name="count">cookie的有效时间(以天为单位)</param>
public static void SetCookie(Microsoft.AspNetCore.Http.HttpContext context, string key, String value, int count = 1)
{
//value = EnDecrypt.Encrypt(value);
CookieOptions options = new CookieOptions();
options.Expires = DateTime.Now.AddDays(count);
if (context.Request.Cookies.ContainsKey(key))
{
options.Expires = DateTime.Now.AddDays(-2);
}
context.Response.Cookies.Append(key, value, options);
}
#endregion
#region 获取cookie的值 public static string GetCookie(string cookieName)
/// <summary>
/// 获取cookie的值
/// </summary>
/// <param name="cookieName">cookie的名字</param>
/// <returns>返回cookie值</returns>
public static string GetCookie(Microsoft.AspNetCore.Http.HttpContext context, string cookieName)
{
string result = "";
context.Request.Cookies.TryGetValue(cookieName, out result);
if (!String.IsNullOrWhiteSpace(result))
{
//result = EnDecrypt.Decrypt(result);
}
return result;
}
#endregion
#region 移除cookie public static void DeleteCookie(string cookieName)
/// <summary>
/// 移除cookie
/// </summary>
/// <param name="cookieName">要移除cookie的名字</param>
public static void DeleteCookie(Microsoft.AspNetCore.Http.HttpContext context, string cookieName)
{
context.Response.Cookies.Delete(cookieName);
}
#endregion
#endregion
}
/* 使用说明
* 使用Session时
* 在Startup类 ConfigureServices(IServiceCollection services) 方法中
* 注册 session服务 (services.AddSession();) 在Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
* 中添加 session 配置 app.UseSession(); 此配置不要加在 if else 条件判断中
*/