[英]Azure C# - How to Get User Details from Bearer Token
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.我正在使用 PostMan 向 API(Function APP URL)发出请求,在 Header 部分我传递了 Bearer 令牌字符串,在正文中传递了 JSON 类型请求以进一步处理。
Now In the code I need to decode the Bearer Token and I need User details to maintain "CreatedBy" and UpdatedBy fields.现在在代码中,我需要解码 Bearer Token,我需要用户详细信息来维护“CreatedBy”和 UpdatedBy 字段。
In real scenario it should work who is invoking the API it should capture his credentials details in those fields.在实际场景中,调用 API 的人应该可以工作,它应该在这些字段中捕获他的凭据详细信息。
My Questions我的问题
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 2) 如何通过传递解码值获取用户详细信息,我认为与此同时我们还需要传递一些更多信息,如客户端 ID、TenantID、Appid 等,我可以从本地/应用程序设置中读取这些信息。Json
Thanks Krishna谢谢克里希纳
Request.Headers["XYZComponent"];
Check for null. How you can get the headers depends on which frameworks you are using to build the server.检查 null。如何获取标头取决于您用于构建服务器的框架。
It's true that you could fetch the token and try to read it, but don't.的确,您可以获取令牌并尝试读取它,但不要这样做。
Install the nuget Microsoft.AspNetCore.Authentication.JwtBearer.安装 nuget Microsoft.AspNetCore.Authentication.JwtBearer。 This will add middleware and auto-validate the tokens just before every request is processed.这将在处理每个请求之前添加中间件并自动验证令牌。
Then you can use the api to extract custom properties from a validated token and add them to the principal identity for the request.然后,您可以使用 api 从经过验证的令牌中提取自定义属性,并将它们添加到请求的主体身份中。 Start by searching on implementing the nuget package.首先搜索实现 nuget package。
Do you mean to read the request Header like this?你的意思是这样读取请求Header吗?
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.