开发者

C# parsing cookie get null

开发者 https://www.devze.com 2023-03-14 12:56 出处:网络
This is my cookie value: email%3Drrr%2540test.com%26password%3Da8f5f167f44f4964e6c998dee827110c and开发者_开发百科 that\'s my code:

This is my cookie value:

email%3Drrr%2540test.com%26password%3Da8f5f167f44f4964e6c998dee827110c

and开发者_开发百科 that's my code:

Request.Cookies["SolidDomain"]["email"];
Request.Cookies["SolidDomain"]["password"];

or

 string email1 = Request.Cookies.Get("SolidDomain").Values.Get("email");
 string password2 = Request.Cookies.Get("SolidDomain").Values.Get("password");

in both cases I get null. what's wrong here?


You can have subkey cookie without problem but I think the string you gave us is not using subkey. In fact, the string you posted can be accessed directly with :

Request.Cookies["email"];
Request.Cookies["password"];

If you can to check to structure of the subkey you can use this snippet of code :

for(int i=0; i<Request.Cookies.Count; i++)
{
    aCookie = Request.Cookies[i];
    output.Append("Name = " + aCookie.Name + "<br />");
    if(aCookie.HasKeys)
    {
        for(int j=0; j<aCookie.Values.Count; j++)
        {
            subkeyName = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
            subkeyValue = Server.HtmlEncode(aCookie.Values[j]);
            output.Append("Subkey name = " + subkeyName + "<br />");
            output.Append("Subkey value = " + subkeyValue + 
                "<br /><br />");
        }
    }
    else
    {
        output.Append("Value = " + Server.HtmlEncode(aCookie.Value) +
            "<br /><br />");
    }
}

And check the output variable to see Value and SubKey value.

If you want to allow cookie to a domain (I guess this is what you are trying to achieve) you can do it by using the domain property :

Response.Cookies["password"].Domain = "SolidDomain";
0

精彩评论

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