从asp.net中的前端cookie中提取细节

时间:2011-06-03 20:29:09

标签: asp.net asp.net-mvc

我有一个身份验证cookie,我想知道是否有可能从代码的asp.net部分(即HTML部分)中的cookie中提取数据。我能够在后端使用它将其拉出来

下面的代码
IPrincipal p = HttpContext.Current.User;
            // p.Identity.Name : this is what we will use to call the stored procedure to get the data and populate it


            string userid = p.Identity.Name;
            Response.Write("Welcome  " +
                p.Identity.Name);
            var dbcontext = new PrepLicensingSolution2010.DAL.LicensingEntities1();
            if (userid != null)
            {
                var user = dbcontext.commissions_proc(userid);
            }

但这可以在前端完成吗?

由于

2 个答案:

答案 0 :(得分:1)

这取决于auth cookie的设置方式。如果使用HTTPOnly,则无法通过javascript或(如果浏览器是安全的)客户端访问它。

要使用Javascript访问Cookie,请使用

var myCookieArray = document.cookie.split(";");

循环遍历数组以找到您要查找的cookie。

此处提供了更多信息:http://www.w3schools.com/JS/js_cookies.asp

答案 1 :(得分:0)

这样的东西
<asp:Label ID="welcome" runat="server" Text='<%= String.Format("Welcome {0}", HttpContext.Current.User.Identity.Name) %>'

在允许加载页面之前测试用户是否实际登录是唯一的事情,因为如果Identity.Name为null,您可能会获得NullReferenceException