C# REST: HttpRequest Headers. "Authorization", $"Bearer..." Need to add AppId and Token

Timothy Fischer 121 Reputation points
2021-08-13T04:27:02.6+00:00

All:

I'm new to REST and need to pass in an AppId and Token. I've tried several different approaches similar to:

       var request = new HttpRequestMessage(new HttpMethod(httpMessageType), 
             requestMessage.RequestUri);

        request.Headers.TryAddWithoutValidation("Accept", "application/json");
        request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {authString}");

I tried entering the url in Edge and received a 200 response with as expected data returned. Essentially I need to make the url look like this after adding the parameters:

https://<Address>/auth/v1/appToken?appId=<AppId string>&Token=<Token string>

Can anyone help me to understand how this can be done? The API is hosted in AWS, if that helps.

Thanks in advance!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,336 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,563 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,071 Reputation points Microsoft Vendor
    2021-08-16T05:32:34.867+00:00

    Hi @Timothy Fischer ,

    Essentially I need to make the url look like this after adding the parameters: https://<Address>/auth/v1/appToken?appId=<AppId string>&Token=<Token string>

    From your description, you want to transfer the parameters via the request URL, in this scenario, you can append the parameter at the end of the request URL, code like this:

        HttpClient client = new HttpClient();  
        client.BaseAddress = new Uri("https://localhost:44310/api/todo/");  
        client.DefaultRequestHeaders  
                .Accept  
                .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header  
    
        //append the parameter at the end of the request url  
        var url = "<requestMessage.RequestUri>" + "?appid=APPidstring$token=jwttoken";  
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);  
    
        //// the following cod will add the jwt token in the http request header.  
        var authString = "jwt token";  
        request.Headers.Add("Authorization", $"Bearer {authString}");  
        //request.Headers.TryAddWithoutValidation("Accept", "application/json");   
        //var authString = "jwt token";  
        //request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {authString}");  
    

    Then, use Fiddler to capthure the http request, the result as below:

    123378-capture.png

    Note By using the above code, the token is added in the request URL, it might cause the 414 URI Too Long error. Generally, the toke is transferred via the Http Request Header, I suggest you could refer the above sample code to transfer the token via the header's Authorization attribute, screenshot as below.

    123390-image.png

    Or you can transfer the token via Http Request body, refer this article:ASP.NET Core 3.1 - JWT Authentication Tutorial with Example API.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

0 additional answers

Sort by: Most helpful