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

I had set a cookie in javascript some thing like thisfunction setCookie(c_name,value,exdays){var exdate

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' )
    ;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744199028a4562794.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信