c# Httpclient authorization header without realm - authorization

Many services I wish to authenticate with require the following format:
authorization: ZCb8p0CIngLSFrBJgA/BYyUZI8zaj3MPg=
If you add a realm in front of the request, it no longer works.
authorization: basic ZCb8p0CIngLSFrBJgA/BYyUZI8zaj3MPg= //doesn't work
Yet, in c# the basic httpclient, I can't add a request header authorization without adding a realm. It always throws an exception, is there a way to add the request authorization header with no realm?
Throws error but this authenticates:
request.Headers.Add("Authorization", "ZCb8p0CIngLSFrBJgA/BYyUZI8zaj3MPg=");
Doesn't throw an error but does not authenticate:
request.Headers.Add("Authorization", "basic ZCb8p0CIngLSFrBJgA/BYyUZI8zaj3MPg=");

client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", String.Format("example;{0}", value));
HttpClient Authorization Header Invalid Format

Related

how to get a 2 legged authentication on autodesk forge

i have generated a token using a get request and when i am making an api call the call is getting failed so pls add up your suggestion's
here is the error where i am getting an error ,here is the access token which is generated from the 2-legged authentication how do i verify the access token and make a api call to the Autodesk forge
http status code 401 means that server doesn't know who you are. it may occur when you didn't put 'Authorization' in your request headers (or maybe in an incorrect form).
so make sure you have put 'Authorization' in your http request headers and try again!
(note that you have to add 'Bearer ' in front of your access token, and there's a blank after 'Bearer')
{
Authorization : 'Bearer ' + <your token>
}

Setting variables from the response in IntelliJ IDEA's HTTP request generator

I am enjoying IntelliJ IDEA's HTTP request generator as an alternative to Postman etc.
Currently, my application uses an ephemeral key for JWTs. You know what kind of pain it is... Every time you restart the application you MUST authenticate again, even if the old token is still (temporally) valid.
Currently I have to run my OAuth authentication request with my credentials, copy-and-paste the JWT from the output JSON, and then paste in into next request's Authorization header
GET http://localhost:8080/api/auth/v1/token
Authorization: CCB [... static token...]
{
"id_token": "eyJhbGciOiJIUzI1NiJ9....", <== copy that!
"refresh_token": "eyJhbGciOiJIUzI1NiJ9....",
"expires_in": 1634292409144,
"user_details": {
And
PATCH http://localhost:8080/api/v1/example/runImportJob
Authorization: Bearer <== paste!
Question
I know that IntelliJ IDEA supports {{variables}}. I'd like to ask if it is possible to set the output of the token invocation into a variable which I'll then reference in the Authorization header
Desiderata
PATCH http://localhost:8080/api/v1/example/runImportJob
Authorization: Authorization {{jwt}}
And to run the authentication request (GET .../token) which ultimately sets the jwt variable, after jsonpath-ing the response of corse
You can try using client.global.set and client.global.get to save/load variables. See the example at https://www.jetbrains.com/help/idea/http-response-handling-examples.html#script-var-example .

ASP.NET WebApi always returns 401 Unauthorized

I am trying to create a new endpoint in my WebApi which will be called from another Server. So, I want to set up the S2S JWT bearer based authentication for my WebApp.
The startup.cs code looks like this
public void Configuration(IAppBuilder app)
{
InitializeLoggers();
HttpConfiguration httpConfiguration = new HttpConfiguration();
WebApiConfig.Register(httpConfiguration);
app.Use<JwtS2SAuthMiddleware>();
app.UseWebApi(httpConfiguration);
}
I have added the new line here app.Use<JwtS2SAuthMiddleware>();
Inside the JwtS2SAuthMiddleware, I do the following checks
context.Request.Path.Value.StartsWith("/newendpoint");
Get the Authorization bearer token from request header
Validate the audience, issuer, issuer signing keys using the JwtSecurityTokenHandler.ValidateToken()
Verify the appId contained in JWT is in the allowed list of App Id's
Upon debugging using a request from Fiddler, I see all the above checks pass but still the response is 401 Unauthorized with a message
{"message":"Authorization has been denied for this request."}
Can someone help if I would need to anything else after doing the checks in my JwtS2SAuthMiddleware class ? Thanks.

Receving oauth 2.0 401 error when trying to retrieve token with authorization code

I am trying to retrieve an authorization code for a token but keep getting a 401 error. I get the authorization code from my callbackUrl.
It believe I am sending the request properly. I am sending a base64 Bearer token in the authorization header using my key:secret. I am just not sure what to do from here to resolve things. I am new to oauth so I don't know if there is a way for me to determine the problem from my end of things? I'm completely stumped...
[callbackUrl]/?scope=READ&state=test&code=98LtBkcY
https://www.[testapi].net/v1/id/oauth/access_token?grant_type=authorization_code&code=98LtBkcY
Error:
HTTP/1.1 401 Unauthorized
Content-Type: application/json
WWW-Authenticate: Bearer realm="null",error='invalid_token",error_description='keymanagement.service.invalid_access_token: Invalid Access Token"
Content-Length: 116
{"fault":{"faultstring":"Invalid Access Token","detail":{"errorcode":"keymanagement.service.invalid_access_token"}}}
I'm working in VB.NET and creating my bearer token like this:
Authorization_Token = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ConsumerKey & ":" & ConsumerSecret)).ToString
I got it working. Their documentation told me to use the token as a Bearer token but tried it and worked with out it.

.Net CF 2.0 HttpWebRequest pre-authentication and sending credentials on first request

I'm attempting to communicate with bit.ly's REST API using their modified version of basic authentication. However in order for this to work HttpWebRequest needs to attach the credentials on the first request, however, HttpWebRequest will not send credentials on the first request and will wait for a 401 before sending any credentials even if PreAuthenticate is set to true (with PreAuthenticate it will send credentials for all subsequent requests).
I have attempted the following to get HttpWebRequest to work the way bit.ly requires it:
(1) Send the request in the format http://username:password#api.bi.ly/method .
Not supported by bit.ly (as this is a fake implementation of basic authentication they only check the header).
(2) Manually inject the "Authorization" header into the HttpWebRequest.
Not possible in the .Net CF as the Authorization header is protected and any attempt to modify a protected header value fails and throws an ArgumentException.
(3) Inherit HttpWebRequest or WebRequest in another class as to implement the required behavior.
Not possible as The HttpWebRequest class is registered to service requests for HTTP and HTTPS schemes by default. Attempts to register a different WebRequest descendant for these schemes will fail as duplicate prefixes are not allowed.
So anyone got any suggestions?
A note on (2). I used the method described at http://blog.kowalczyk.info/article/Forcing-basic-http-authentication-for-HttpWebReq.html to inject an authorization header and it works fine in .NET CF 2.0.
public void SetBasicAuthHeader(WebRequest req, String userName, String userPassword)
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
}