Extract cookies info from response headers C# - c#

I have some information that is available in a response header. One way would be to come up with a regex for it...Is there another way to work around this?
This is the header info:
Headers = {Cneonction: close
Content-Length: 87
Content-Type: application/json;charset=utf-8
Date: Mon, 11 Feb 2013 05:51:37 GMT
Expires: Thu, 01-Jan-1970 00:00:00 GMT
Set-Cookie: FUTWebPhishing-123456789=123456aavbc12334564856;Path=/;Domain=.ea.com;Expires=Wed, 1...
I want the value of FUTWebPhishing..Any ideas?

You can string.Split(';') the value of the Set-Cookie header and then find the element that contains "FUTWebPhishing".

There is a discussion on SO regarding this issue
Check it out:
GetCookie extract information to a String

Related

Is it possible to change the order of the Headers in a HttpWebResponseMessage using the ApiController?

I have a legacy project which uses .NET Framework 4.5.2 and NancyModule.
When I get the result of a GET-request, then the Headers have the following order:
Key
Value
Content-Length
206
Content-Type
application/json; charset=utf-8
Vary
Accept
Server
Microsoft-HTTPAPI/2.0
Link
</Servicename.xml>; rel="application/xml"
x-powered-by
...
Date
Tue, 31 Jan 2023 13:25:07 GMT
I transfer this project to .net 6 and Microsoft.AspNetCore.Mvc.
When I get the result of a GET-request, then the keys of the Headers are arranged alphabetically.
This leads me to the following question:
Is it possible to change the order of the headers?
I tried to remove and add several values of the dictionary in HttpContext.Response.Headers but it has no entries. When I added a custom header then it was also in alphabetical order.
If you are using Kestrel (the default web server in ASP.NET Core) you might want to remove the Server header in order to try to have the Date header last.
But that would be very fragile, you can't really control the order of the headers, see the source code of how it's done!
For simple HTTP responses that don't set any special headers, this might work and you might end up with something like that. Note the many conditionals used in the previous sentences. 😉
HTTP/1.1 200 OK
Content-Length: 4536
Content-Type: text/plain
Date: Tue, 31 Jan 2023 14:58:31 GMT
And here's how to disable the Server header for Kestrel:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false);
If you need to use HTTP.sys instead of Kestrel then you'll be out of luck since the Content-Length header is added after the Date header and there's nothing you can do about it.
HTTP/1.1 200 OK
Content-Type: text/plain
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 31 Jan 2023 15:11:23 GMT
Content-Length: 4536

400-Bad request- request header to long

I'm trying to generate pdf,but after like the 5th page header and footer display http error 400-bad request-request header to long. is there anyway to solve this problem? a response header can look like
this
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: // i hid the adress
Cache-Control: no-store, no-cache
Content-Disposition: attachment; filename=Utskrift.pdf
Content-Length: 162568
Content-Type: application/pdf
Date: Tue, 25 Oct 2022 08:57:16 GMT
Expires: -1
Pragma: no-cache
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
The length you are trying to send is very large to be handled. It is like overdosing the request header. What you can do is, you can consider to break it in to chunks (smaller sizes), and send them separately until you gather all the file size on the endpoint you are sending.

C# MVC RouteBase routing too many redirects

I have this custom routing system where i will get the paths, controllers, views and areas from my DB and set them according to the requested path.
My problem right now is that when i try to access one page it gives me the too many redirects response.
What happens in this area is:
User access page and fill a form;
Form is posted using AJAX and then a redirect is made from jquery;
User makes an appointment or generate a voucher;
If the user tries to return to the previous page by typing it on the browser( i don't have any button to that link ) he gets the too many redirects problem.
Since the code is kind of big i'm going to post it here: http://pastebin.com/yTdWKMp4
I only left out my DB logic.
What could be doing this ? I only could see the problem in this area but i don't know for sure it isn't happening in other areas.
EDIT
These are the headers from the requests
HTTP/1.1 302 Found
Cache-Control: private, no-store, max-age=1
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Wed, 25 Nov 2015 19:01:23 GMT
Last-Modified: Wed, 25 Nov 2015 19:01:22 GMT
Etag: ""
Location: /teste-lp
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-UA-Compatible: IE=Edge,chrome=1
Date: Wed, 25 Nov 2015 19:01:22 GMT
Content-Length: 115
HTTP/1.1 200 OK
Cache-Control: private, no-store, max-age=1
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Wed, 25 Nov 2015 19:02:10 GMT
Last-Modified: Wed, 25 Nov 2015 19:02:09 GMT
Etag: ""
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-UA-Compatible: IE=Edge,chrome=1
Date: Wed, 25 Nov 2015 19:02:12 GMT
Content-Length: 6852
EDIT 2
After much debugging and logging i found the beginning of the problem, it's related to a redirect i have when a certain session is null:
if (TempData["LeadID"] == null || Session["UnidadeCE"] == null)
{
Response.Redirect( HelperMethods.CreateLink( Request.RawUrl.TrimStart( '/' ).Split( '/' )[0] ) );
Response.End();
return null;
}
I have this verification at the appointment and voucher, if he tries to reload the page he is redirected to the form page. Now what is weird is, when he changes URL the RouteBase executes and tries to fetch the PageInfo( VirtualPath, Controller and View ) for that new URL and at the moment of my search for some reason i'm getting the page of appointment instead of the form one so i get back at the same page and the looping starts.
I have updated my pastebin with the filtering and DB Search, there are no repeated records in my DB. It looks like some sort of freaking cache but the var is local and there's no sharing.
EDIT 3
After some var watching ( i have nearly 6k routes ) i found that the problem lies between my route checking on the pastebin file on line 128, that pageList parameter is my list of all routes from the cache, i do a url search based and then i take the route that i need, on line 167 is where my problem is located. At that moment i have copied the content of the route i want into my freakingPage var (not so cool, i know) and then i change the values of Action and Controller but what also happens is the value changes on the pageList var and also at the Cache, line 203.
What could be causing this ?
After some research I found that my problem was related to the fact that I wasn't using Clone on the values that were coming from the cache. Since the .NET cache keeps a reference value when you get its value, it's necessary to implement ICloneable on the object you are getting from it.
Since I was only "putting" the value on another var and later editing this var I was in fact changing the original value on the cache... and with that I was getting a wrong return when I would hit the cache again.
In my case I was getting a redirect because of some Session verification that I was doing on my Controller, on the first time (freshly made cache) I would hit the correct page. But after I would try to re-enter the same page, I would get a different value from it.
To correct it I implemented ICloneable on my Model. I have also changed my example with the new approach if anyone also have this problem.
http://pastebin.com/yTdWKMp4

Twitter API, Tweets with Images, Error Code 324 "The validation of media ids failed."

I have been able to successfully post status updates with images attached. I am using C#/.NET with a REST framework called Hammock. First, I upload the images to upload.twitter.com/1.1/media/upload.json and get media_ids back. Then, I make a request to api.twitter.com/1.1/statuses/update.json with the media_ids parameter. This works fine except that occassionally, with larger images, Twitter responds to my status update request with the following JSON:
{"errors":[{"code":324,"message":"The validation of media ids failed."}]}
I have tried waiting several minutes between the time of media upload and status update with no success. I have also posted on Twitter's developer forums, but posting here as well in case anyone has experienced this and has any insight. https://twittercommunity.com/t/the-validation-of-media-ids-failed-error-code-324/29304 I don't see "324" anywhere in Twitter's API documentation, especially here: https://dev.twitter.com/overview/api/response-codes
Here are the exact (except for sensitive values being xxxx'd out) requests/responses seen in Fiddler:
Media Upload Request
POST https://upload.twitter.com/1.1/media/upload.json HTTP/1.1
Authorization: OAuth oauth_consumer_key="xxxxxxxxxxx",oauth_nonce="xxxxxxxxxxx",oauth_signature="xxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1418763417",oauth_token="xxxxxxxxxx",oauth_version="1.0a"
Content-Type: multipart/form-data; boundary=5fb9e9e7-25cf-49fd-b5ef-61aefed2897e
Host: upload.twitter.com
Content-Length: 2399916
--5fb9e9e7-25cf-49fd-b5ef-61aefed2897e
Content-Disposition: form-data; name="media"; filename="d1cd395d-bebf-4733-aedc-680327ef63ec"
Content-Type: image/jpeg
Media Upload Response
HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-length: 140
content-type: application/json;charset=utf-8
date: Tue, 16 Dec 2014 20:56:57 UTC
expires: Tue, 31 Mar 1981 05:00:00 GMT
expires: Tue, 16 Dec 2014 21:56:57 GMT
last-modified: Tue, 16 Dec 2014 20:56:57 GMT
pragma: no-cache
server: tsa_b
set-cookie: lang=en
set-cookie: guest_id=v1%xxxxxxxx; Domain=.twitter.com; Path=/; Expires=Thu, 15-Dec-2016 20:56:57 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read-write
x-connection-hash: xxxxxxxxxxx
x-frame-options: SAMEORIGIN
x-response-time: 344
x-transaction: xxxxxxxxx
x-xss-protection: 1; mode=block
{"media_id":544959385899122689,"media_id_string":"544959385899122689","size":4311817,"image":{"w":3000,"h":2350,"image_type":"image\/jpeg"}}
Status Update Request
POST https://api.twitter.com/1.1/statuses/update.json HTTP/1.1
Authorization: OAuth oauth_consumer_key="xxxxxxxxxx",oauth_nonce="xxxxx",oauth_signature="xxxxxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1418763469",oauth_token="xxxxxxxxxxx",oauth_version="1.0a"
Content-Type: application/x-www-form-urlencoded
Host: api.twitter.com
Content-Length: 117
status=test%20test%20test&media_ids=544959385899122689%2C544959443889557504%2C544959458036957184%2C544959545941192705
Status Update Response
HTTP/1.1 400 Bad Request
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-length: 73
content-type: application/json;charset=utf-8
date: Tue, 16 Dec 2014 20:57:49 UTC
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Tue, 16 Dec 2014 20:57:49 GMT
pragma: no-cache
server: tsa_b
set-cookie: lang=en
set-cookie: guest_id=xxxxxx; Domain=.twitter.com; Path=/; Expires=Thu, 15-Dec-2016 20:57:49 UTC
status: 400 Bad Request
strict-transport-security: max-age=631138519
x-access-level: read-write
x-connection-hash: xxxxxxxx
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 44
x-transaction: xxxxxxx
x-xss-protection: 1; mode=block
{"errors":[{"code":324,"message":"The validation of media ids failed."}]}
This is a result of the image being too large. I just ran in to the same issue, and resolved it by sending along a jpg version (300KB) of a largeish png (it was 1.2MB).
The stated size limit is 3MB, but they might have some kind of client-specific throttling in place.

oAuth 2.0 Google API asp.net Requesting Token

i have a problem getting token from google api, first i make this request to get code from google
accounts.google.com/o/oauth2/auth?scope=www.googleapis.com/auth/userinfo.profile www.googleapis.com/auth/userinfo.email&state=profile&redirect_uri={http://pedidostogo.com/Login.aspx}&response_type=code&client_id=519970867780.apps.googleusercontent.com&access_type=offline
this seems to be working fine (im getting a code from google) after that i make another request to get token using that code, for this i make:
accounts.google.com/o/oauth2/token?client_secret={my client secret}&grant_type=authorization_code&redirect_uri={http://pedidostogo.com/Login.aspx}&client_id={my client id}&code={code i get from google}
but when i do that POST request im getting:
Status:400 Bad Request
Headers: Pragma: no-cache
Date: Thu, 28 Feb 2013 20:00:00 GMT
Content-Encoding: gzip
X-Content-Type-Options: nosniff
Server: GSE
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Content-Type: application/json
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
X-XSS-Protection: 1; mode=block
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Data: {
"error" : "invalid_request"
}
can anyone tell me what im doing wrong? thanks.

Categories

Resources