使用CookieHelper帮助类:

public class CookieHelper
{ #region 获取Cookie /// <summary>
/// 获得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[cookieName]);
return "";
} /// <summary>
/// 获得Cookie的值
/// </summary>
/// <param name="cookie"></param>
/// <returns></returns>
public static string GetCookieValue(HttpCookie cookie)
{
if (cookie != null)
{
return cookie.Value;
}
return "";
} /// <summary>
/// 获得Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookie(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
} #endregion #region 删除Cookie /// <summary>
/// 删除Cookie
/// </summary>
/// <param name="cookieName"></param>
public static void RemoveCookie(string cookieName)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
response.Cookies.Remove(cookieName);
}
}
} #endregion #region 设置/修改Cookie /// <summary>
/// 设置Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName,string value, DateTime? expires)
{
Guard.IsNotNullOrEmpty(cookieName, "cookieName"); HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
response.SetCookie(cookie);
}
} } #endregion #region 添加Cookie /// <summary>
/// 添加为Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string cookieName, string value, DateTime expires)
{
Guard.IsNotNullOrEmpty(cookieName, "cookieName"); HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
cookie.Value = value;
AddCookie(cookie);
} /// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookie"></param>
public static void AddCookie(HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
//指定客户端脚本是否可以访问[默认为false]
cookie.HttpOnly = true;
//指定统一的Path,比便能通存通取
cookie.Path = "/";
//设置跨域,这样在其它二级域名下就都可以访问到了
//cookie.Domain = "nas.com";
response.AppendCookie(cookie);
}
} #endregion
}

设置cookie:

   public static string VCLoginName
{
get
{
try
{
return CookieHelper.GetCookieValue("VCLoginName").ToStr();
}
catch
{
return "";
}
}
set
{
var cookie = CookieHelper.GetCookie("VCLoginName");
if (cookie != null)
{
CookieHelper.SetCookie("VCLoginName", value, DateTime.Now.AddHours());
}
else
{
//有效期,一个钟头
CookieHelper.AddCookie("VCLoginName", value, DateTime.Now.AddHours());
} }
}

设置:

 SysContext.VCLoginName = user.LoginName;

js获取和设置cookie:

 function getCookie(cname) {
console.log("开始")
console.log(document.cookie)
console.log("结束")
var arrStr = document.cookie.split("; "); for (var i = 0; i < arrStr.length; i++) { var temp = arrStr[i].split("="); if (temp[0] == cname) return unescape(temp[1]); } } //添加cookie
function addCookie(cname, cvalue, ctime) {
var str = cname + "=" + escape(cvalue);
if (ctime > 0) { //为时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = ctime * 3600 * 1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
alert(cname + cvalue);
}
document.cookie = str;
}

遇到的问题:

后台设置cookie但是通过js无法获取相应的cookie的值,why?

不知道。但是自己在一般处理程序中写的cookie却可以通过js获得。why?

 HttpCookie cookie = new HttpCookie("LoginName", HttpUtility.UrlEncode(username)); //定义cookie对象以及名为DocUrl的项
DateTime dt = DateTime.Now; //定义时间对象
TimeSpan ts = new TimeSpan(, , , ); //天,小时,分钟,秒 ,cookie有效作用时间
cookie.Expires = dt.Add(ts); //添加作用时间
context.Response.AppendCookie(cookie); //确定写入cookie中

解决:

因为CookieHelper中设置了cookie的HttpOnly为true。

ASP.Net中HttpCookie对象的HttpOnly 属性 指定一个Cookie 是否可通过客户端脚本访问。不能通过客户端脚本访问为 true;否则为 false。默认值为 false。此属性并不能完全阻止客户端在本地获取cookies,但是可以增加通过脚本直接获取的难度。

js无法获取.net设置的cookie的更多相关文章

  1. js | javascript获取和设置元素的属性

    获取和设置元素的内容: var nv = document.getElementById("pid"); alert(nv.innerHTML); nv.innerHTML=&qu ...

  2. js操作获取和设置cookie

    //创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeU ...

  3. js / jquery 获取和设置 FCK Editor 的值

    开发中遇到 通过 $("#content").val(); 或者 document.getElementById("content"); 并不能获取到 id 为 ...

  4. js 动态获取高度 设置距离

    var boxs = document.getElementById('m2-head');var Height = boxs.clientHeight||o.offsetHeight;console ...

  5. php 设置中文 cookie, js获取

    参考链接:http://www.nowamagic.net/librarys/veda/detail/1271 http://www.ruanyifeng.com/blog/2008/06/base6 ...

  6. Js添加、读取、删除cookie,判断cookie是否有效,指定domain域下主路径path下设置cookie,设置expires过期时间

    有时我们需要用cookie保存用户名,记录登录状态,如何正确判断该机用户cookie是否存在呢?不能简单使用a!=”这样的写法. 正确方法是:判断是否存在名为username3的cookie,使用do ...

  7. js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq

    js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...

  8. js/java 获取、添加、修改、删除cookie(最全)

      一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...

  9. 怎样指定当前cookie不能通过js脚本获取

    所谓" 不能通过js脚本获取 " 主要指的是: 使用document.cookie / XMLHttpRequest对象 / Request API 等无法获取到当前cookie. ...

随机推荐

  1. 『动态』动态JSON万能转换函数 + .Net40 dynamic动态数据绑定

    不废话,调用代码: static void Main(string[] args) { string json = File.ReadAllText("2.txt", Encodi ...

  2. 从零开始学TensorFlow

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 最近在学习TensorFlow的相关知识,了解了Te ...

  3. 设计模式(Design Patterns)的简单讲解

    模式的诞生与定义 模式(Pattern)起源于建筑业而非软件业(小本本记下来--) 模式之父--美国加利佛尼亚大学环境结构中心研究所所长Christopher Alexander博士; 模式 : -C ...

  4. <pre>标签:\r\n换行符的保留

    mysql数据读库的\r\n换行符处理 这个问题是我在采集数据时发现的,采集网页的数据,大概有6千多条,采集的内容保留了最原始的\r\n和\n换行字符,但在mysql管理工具中(phpmyadmin和 ...

  5. #ifndef, #define, #endif 作用

    #ifndef, #define, #endif 作用 https://www.cnblogs.com/challenger-vip/p/3386819.html

  6. 恢复oracle中误删除drop掉的表 闪回的方法

    恢复oracle中误删除drop掉的表   查看回收站中表 --需要在其所在用户下查询 回收站对象 select object_name,original_name,partition_name,ty ...

  7. ASP.NET Core 身份验证(一)

    前言 这篇文章我想带领大家了解一下 ASP.NET Core 中如何进行的身份验证,在开始之前强烈建议还没看过我写的 Identity 系列文章的同学先看一下. Identity 入门系列文章: Id ...

  8. 神奇的选择器 :focus-within

    CSS 的伪类选择器和伪元素选择器,让 CSS 有了更为强大的功能. 伪类大家听的多了,伪元素可能听到的不是那么频繁,其实 CSS 对这两个是有区分的. 有个错误有必要每次讲到伪类都提一下,有时你会发 ...

  9. 【死磕 Spring】----- IOC 之 加载 Bean

    原文出自:http://cmsblogs.com 先看一段熟悉的代码: ClassPathResource resource = new ClassPathResource("bean.xm ...

  10. Intellij IDEA 阅读源码的 4 个绝技,我必须分享给你!

    前段时间分享了<阅读跟踪 Java 源码的几个小技巧>是基于 Eclipse 版本的,看大家的留言都是想要 IDEA 版本的源码阅读技巧. 所以,为了满足众多 IDEA 粉丝的要求,栈长我 ...