Adding authorization to the headers

前端 未结 5 1369
悲&欢浪女
悲&欢浪女 2020-12-09 03:03

I have the following code:

...
AuthenticationHeaderValue authHeaders = new AuthenticationHeaderValue(\"OAuth2\", Contract.AccessToken);
string result = await         


        
5条回答
  •  被撕碎了的回忆
    2020-12-09 03:20

    Had a similar issue when getting AuthenticationHeaderValue to work with my requests. I was also using JWT JsonWebToken from GitHub. I was able to get a token from the API, but was struggling to use it in other GETs and POSTs.

    var jwt = JsonWebToken.Encode(token, APISECRET, JwtHashAlgorithm.HS256);
    var tk = GetTokenFromApi(); // basically returns an encrypted string.
    

    Manually using WebRequest: Which worked fine.

    request.ContentType = "application/json";
    request.Method = "POST";
    request.Headers.Set("Authorization", string.Format("Bearer {0}", tk));
    

    When we switched to an HttpClient, and used the AuthenticationHeaderValue, could not figure out how to set it up correctly.After looking at the request string, i saw it added the "Authorization" for me. Played around with parameters, and this finally this worked.

     var authenticationHeaderValue = new AuthenticationHeaderValue("Bearer", tk);
    

提交回复
热议问题