Exception calling Add. Part of cookie is invalid - c#

I am using the Add method of System.Net.CookieContainer. It has worked well over the years but suddenly I am getting:
Exception calling "Add" with "2" argument(s): "The
'Value'='321,386,%2F%3Fa%3D1,http%3A%2F%2Fwww.xxxx.com%2Fpremium%2Fmoney'
part of the cookie is invalid."
I was adding a cookie returned from a web page. The raw header from the web page is:
...
_chartbeat_uuniq=1;
_chartbeat5=321,386,%2F%3Fa%3D1,http%3A%2F%2Fwww.xxx.com%2Fpremium%2Fmoney;
gs_p_GSN-375009-Z=0;
...
What is wrong with the cookie value? Is it the comma?

You should encode the cookie value. The best way is by using UrlEncode. Check this out.
HttpServerUtility.UrlEncode

Related

Google People API - listDirectoryPeople pageToken always invalid

The pageToken I receive on the first call, always causes HTTP 400 on my second call.
Message
Page token is invalid. Retry call without the page token.
Status
INVALID_ARGUMENT
When using the exact same pageToken in the "Try this API" of the documentation, it works.
https://developers.google.com/people/api/rest/v1/people/listDirectoryPeople?hl=zh-tw
Why does it fail?
The pageToken parameter must be URL-encoded, for C# this would be:
HttpUtility.UrlEncode(nextPageToken)
If you want to test with Postman, use an online version, like the "Try it yourself" of the w3school article on the matter:
https://www.w3schools.com/tags/ref_urlencode.ASP

How to set a raw cookie value in asp.net core?

I would like to set the value of mycookie to value=somevalue, thus:
The recommended approach of using Cookies.Append does not work, as the value is URL encoded and ends up being value%3Dsomevalue.
My code for adding the cookie:
_context.Response.Cookies.Append("mycookie", $"value=somevalue");
My code for reading the cookie
_context.Request.Cookies["mycookie"]
The question: what would be the best way to bypass the URL encoding and set the raw value of the cookie?
I found a reasonable workaround, by setting the cookie header manually.
_context.Response.Headers.Append("Set-Cookie", "mycookie=value=somevalue");
You can use the same code to get the cookie back:
_context.Request.Cookies["mycookie"]

AWS Cloudfront returning Missing Key-Pair-Id query parameter or cookie value

I have the code that returns the Cloud Front Signed Cookie Values.
CookiesForCustomPolicy signedCookiesUrl = AmazonCloudFrontCookieSigner.GetCookiesForCustomPolicy("https://example.cloudfront.net/movies/nature.mp4", new StreamReader(File.OpenRead(Path.Combine(AppContext.BaseDirectory, "pk-2.pem"))),"APKEXAMPLEKEYID", DateTime.Now.AddDays(10), DateTime.Now, null);
I use the returned values to request the object, however returns the
<Error>
<Code>MissingKey</Code>
<Message>
Missing Key-Pair-Id query parameter or cookie value
</Message>
</Error>.
I test this through the PostMan tool putting the headers and direct request through Chrome browser and still getting the same error.
I have use the correct Cloudfront Key Pair and correct resource URL. My objects are private and cloudfront have access to it. Is there any thing else that i need to work on to get this working?
Add Header Key Pair
These aren't raw headers, they're cookies. Although I don't use postman, it sounds like this is your issue:
Based on what you've said, you wouldn't add them like this:
[CloudFront-Key-Pair-Id, APKEXAMPLEQQ]
Instead it should look more like this:
[Cookie, CloudFront-Key-Pair-Id=APKEXAMPLEQQ]
Here is the example for Signed URL in C#.
http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CreateSignatureInCSharp.html
When you return the signed keys, you can return with query string parameter or Cookies.
You can return cookies to API Gateway in two ways, Do with ANY Integration and return the headers as it is.
If you do any other method, you need to return json data and map json data to headers in API Gateway.
http://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html#mapping-response-parameters
Hope it helps.
When we use PUBLIC_KEY and PRIVATE_KEY then we get Key-Pair-Id missing. We should use Access Key Id instead of PUBLIC_KEY then it will work perfectly.

How do you put an authentication token on URL as query parameter?

I'm doing a PayPal Express Checkout on my ASP.Net MVC site.
The site uses token authentication so I'm trying to put the token on the PayPal return URL as a query parameter.
I have a handler that intercepts all requests to my site and extracts the token from the URL or the request header.
Works fine getting it from the header but I get the following exception when its a query parameter. I get the token from my claims principal and don't do any encoding/decoding. I've tried this method but had no luck (same exception occurs).
System.ArgumentException was caught
HResult=-2147024809
Message=Jwt10204: 'System.IdentityModel.Tokens.JwtSecurityTokenHandler' cannot read this string: '{really long token}'.
The string needs to be in compact JSON format, which is of the form: '<Base64UrlEncodedHeader>.<Base64UrlEndcodedPayload>.<OPTIONAL, Base64UrlEncodedSignature>'.
Source=System.IdentityModel.Tokens.Jwt
StackTrace:
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ReadToken(String jwtEncodedString)
at {our namespace}.Providers.JwtTokenServiceProvider.GetToken(String value)
at {our namespace}.TokenAuthorisationController.Post(TokenRequest request)
Turns out the problem was that PayPal appending another string (that look like a PayPal Express Checkout token) on then end of the URL with a comma as a delimiter instead of the "&" symbol.
So I solved the issue by removing the comma character and the extra characters after the comma to extra my JWT token.

Why Am I getting: "A potentially dangerous Request.Path value was detected from the client (&)."?

I don't understand why I am getting exception calling the action from my controller by typing the full url
It works fine calling from form or ajax post using jquery, it is not setup to accept only post, my last try, I just specify explicitly the HttpGet and setip the validation page = false in the web.config. I'm lost
this is the url I am passing: main/request/theprogram=xx&theaction=yyy&theobject=
exception:
A potentially dangerous Request.Path value was detected from the client (&).
You are passing & as part of "path" portion of Uri which is very unusual and triggers the warning.
Most likely you want it to be part of "query" portion (*notice ? that separates query portion):
main/request/?theprogram=xx&theaction=yyy&theobject=
If you want parameters to be part of the path then it normal to not have names, but simply positioned values or use path-safe separator like ():
main/request/xx/yyy/
main/request/theprogram(xx)/theaction(yyy)/theobject()

Categories