最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get key Value from cookies which was set in javascript, asp.net mvc3 - Stack Overflow

programmeradmin4浏览0评论

I had set a cookie in javascript some thing like this

   function setCookie(c_name,value,exdays)
    {
       var exdate=new Date();
       exdate.setDate(exdate.getDate() + exdays);
       var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
       document.cookie = c_name + "=" + c_value;
    }

var  loginval = slmgusername=swetha.p + slmguserpassword = 12345678;
 setCookie("slmgusercredentails", loginval, 100);

And i controller i am getting values in my cookie like this

 HttpContext.Request.Cookies["slmgusercredentials"].Values = {slmgusername%253Dswetha.p%2526slmguserpassword%253D12345678}

when i am trying to get the username from this some thing like

 UserName = HttpContext.Request.Cookies["slmgusercredentials"].Values["slmgusername"].

I cant able to get the UserName. As i think so the values are in javscript coding format. How to get the username... can any one help me to find the solution...

I had set a cookie in javascript some thing like this

   function setCookie(c_name,value,exdays)
    {
       var exdate=new Date();
       exdate.setDate(exdate.getDate() + exdays);
       var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
       document.cookie = c_name + "=" + c_value;
    }

var  loginval = slmgusername=swetha.p + slmguserpassword = 12345678;
 setCookie("slmgusercredentails", loginval, 100);

And i controller i am getting values in my cookie like this

 HttpContext.Request.Cookies["slmgusercredentials"].Values = {slmgusername%253Dswetha.p%2526slmguserpassword%253D12345678}

when i am trying to get the username from this some thing like

 UserName = HttpContext.Request.Cookies["slmgusercredentials"].Values["slmgusername"].

I cant able to get the UserName. As i think so the values are in javscript coding format. How to get the username... can any one help me to find the solution...

Share Improve this question edited May 8, 2012 at 4:52 Swetha Bindu asked May 7, 2012 at 14:45 Swetha BinduSwetha Bindu 6492 gold badges9 silver badges29 bronze badges 1
  • Please tag this question with your back-end framework (ASP?). It helps to mention it explicitly in the question text, too. – apsillers Commented May 7, 2012 at 14:53
Add a ment  | 

2 Answers 2

Reset to default 5

This should do the trick!

 function ReadCookie()
    {
       var allcookies = document.cookie;
       alert("All Cookies : " + allcookies );

       // Get all the cookies pairs in an array
       cookiearray  = allcookies.split(';');

       // Now take key value pair out of this array
       for(var i=0; i<cookiearray.length; i++){
          name = cookiearray[i].split('=')[0];
          value = cookiearray[i].split('=')[1];
          alert("Key is : " + name + " and Value is : " + value);
       }
    }

Solves the expired problem from accepted answer.

function readKeyValuesFromCookie(){
    return document.cookie
        .split( ';' )
        .map( pair => pair.split( '=' ) )
        .filter( pair => pair.length == 2 )
        .map( pair => [ pair[0].trim(), pair[1].trim() ] )
        .filter( pair => pair[0] != 'expires' )
    ;
}
发布评论

评论列表(0)

  1. 暂无评论