开发者

Extracting details from a cookie in the front end in asp.net

开发者 https://www.devze.com 2023-03-10 12:39 出处:网络
I have a authentication cookie and I was wondering if it would be possible extract data from the cookie in the asp.net part of the code (i.e in the HTML part) .I am able to pull it out in the backend

I have a authentication cookie and I was wondering if it would be possible extract data from the cookie in the asp.net part of the code (i.e in the HTML part) .I am able to pull it out in the backend using the code below

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);
 开发者_高级运维           }

But can this be done in the front end?

Thanks


That depends on how the auth cookie is set. If HTTPOnly is used it cannot be accessed by javascript or (if the browser is secure) client side.

To access a cookie in Javascript just use

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

And loop through the array to find the cookie you are looking for.

More info available here: http://www.w3schools.com/JS/js_cookies.asp


Something like

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

Only thing is to becareful to test to see if the user is actually logged in before allowing the page to load because you COULD get a NullReferenceException if Identity.Name is null

0

精彩评论

暂无评论...
验证码 换一张
取 消