Last post Sep 26, 2019 11:24 AM by AnyUserNameThatLetsMeIn
Sep 25, 2019 10:35 AM|AnyUserNameThatLetsMeIn|LINK
I posted a question here: https://social.msdn.microsoft.com/Forums/en-US/cbc8785a-0c51-47ef-b228-f5e829e358f1/using-jwt-and-connecting-to-server?forum=csharpgeneral
I knew it would be in the wrong place because no matter where I think it should go, someone else has a different opinion, so I am posting it here in its entirety.
I've read up on JWT and in principle it seems reasonably straightforward. The problem I have is that I cannot find decent examples of what I am trying to do. First of all, I am writing a C# application. It is the client. My customer has a web API where I
used to send data to it, and they have now asked that I modify the code in my client to connect to their server using JWT. They have given me a URL and if I use Postman and hit that URL, add in the supplied user name and password, I get back this:
So, I know the supplied details work. What I am trying to do now is work out how I am supposed to use that within my client application. If there is an error (let's say the supplied details aren't right, then I get this response:
"Text": "Unauthorized Access : Please check token/user credentials"
So, at the moment, I have not installed any JWT Nuget packages as there are loads and I cannot find any reason to choose one over the other. All examples I read on the net are about how to write the server side bit and nothing that I can find that shows
how to be the consumer.
My client application is run via a Windows scheduled task. It runs twice a day. All it does is pull data from a Sage accounts system and constructs a bunch of objects which are then serialised into XML and sent to the customer. All that bit works and I can
still get the serialised XML string, but I just don't know how to connect to the server using JWT and send it and manage the responses whether it is an error or success. Any pointers from anyone about how to post to a web service that uses JWT?
Sep 25, 2019 12:02 PM|mgebhard|LINK
Pretty simple really. First get a token from the token endpoint. Seems that you can do that already? Then add the token to the HTTP Authorization header when requesting secured resources. Its' not real clear what HTTP Client you are using but adding
a header is pretty simple just read the docs.
Authorization: Bearer eyJhbGciOiJSUzI1NiJ9....
The token response also contains the expiration. You need expiration so you know when to request a new token. BTW, persisting the token is up to you.
JWTs are very common and the Internet is full of information.
Sep 25, 2019 01:26 PM|AnyUserNameThatLetsMeIn|LINK
Cheers. I wasn't sure if I needed a Nuget package or whether I just deal with it directly myself. I know the web is full of info, but most of what I've found shows how you deal with the server side of the coding and not the client side. Obviously not using
the correct search terms or something.
Sep 25, 2019 02:23 PM|AnyUserNameThatLetsMeIn|LINK
Picking the token up now. Just testing the different responses. I will test the sending using the token later. Thanks for your help.
Sep 26, 2019 11:24 AM|AnyUserNameThatLetsMeIn|LINK
All good now. Picking the token up and I can now send the XML data with the token in the header.