Home > Net >  Azure C# - How to Get User Details from Bearer Token
Azure C# - How to Get User Details from Bearer Token

Time:02-23

I am using PostMan to make a request to API(Function APP URL), in the Header section I am passing the Bearer token string and in the body the JSON type Request to process the further.

Now In the code I need to decode the Bearer Token and I need User details to maintain "CreatedBy" and UpdatedBy fields.

In real scenario it should work who is invoking the API it should capture his credentials details in those fields.

My Questions

  1. How to Get the Bearer Token from the Headers so that I can pass it to decode to get the APP ID and OID/Object ID

2)How to Get User Details by passing the Decode values and I think along with this we also need to pass some more information like Client ID, TenantID, Appid etc which I can read from the Local/App settings.Json

Thanks Krishna

CodePudding user response:

Do you mean to read the request Header like this?

public string Postsam([FromBody]object jsonData)
{
     var re = Request;
     var headers = re.Headers;

    if (headers.Contains("Bearer"))
    {
        string token = headers.GetValues("Bearer").First();
    }

    return null;
}

CodePudding user response:

  1. if you can get HttpRequest then you can get the header by

Request.Headers["XYZComponent"];

Check for null! How you can get the headers depends on which frameworks you are using to build the server.

  1. Additional Infos can be transported via additional claims inside the JWT. You have to decode the token first, then you can access the claims.
  • Related