简体   繁体   English

Azure C# - 如何从不记名令牌中获取用户详细信息

[英]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我的问题

  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如何从标头中获取 Bearer Token,以便我可以将其传递给解码以获取 APP ID 和 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 2) 如何通过传递解码值获取用户详细信息,我认为与此同时我们还需要传递一些更多信息,如客户端 ID、TenantID、Appid 等,我可以从本地/应用程序设置中读取这些信息。Json

Thanks Krishna谢谢克里希纳

  1. if you can get HttpRequest then you can get the header by如果您可以获得 HttpRequest,那么您可以通过以下方式获得 header

Request.Headers["XYZComponent"];

Check for null. How you can get the headers depends on which frameworks you are using to build the server.检查 null。如何获取标头取决于您用于构建服务器的框架。

  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.附加信息可以通过 JWT 内的附加声明传输。您必须先解码令牌,然后才能访问声明。

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.

相关问题 如何在 Azure Log Analytics 中配置基本日志 - 如何获取不记名令牌? - How to configure Basic Logs in Azure Log Analytics - how to get bearer token? 如何通过 azure 数据工厂生成不记名令牌 - How to generate bearer token via azure data factory 从 Azure 监视器工作簿中获取异常详细信息 - Get exception details from a Azure Monitor Workbook 如何使用 C# 从 Azure Devops 中的 Artifacts/Nuget Feed 获取所有包及其版本的列表? - How to get the list of all the packages with their versions from Artifacts/Nuget Feed in Azure Devops using C#? 使用委托身份验证获取不记名令牌 - Get bearer token using delegated authentication C#用户登录email时如何获取用户UID - How to get user UID when user is signed in with email in C# 如何从 AWS Cognito 上的托管 UI 获取用户池令牌 - How to get the User pool token from Hosted UI on AWS Cognito 是否可以在不使用 node.js 的情况下从 javascript 调用 login.microsoftonline.com 获取 Bearer Token - Is it possible to call login.microsoftonline.com from javascript to get Bearer Token without using node.js 如何仅使用用户名(电子邮件)从 AWS Amplify (Auth) 获取用户信息(所有详细信息)? - How can I get user info (all details) from AWS Amplify (Auth) with only username(email)? 从 C# 调用 Google OAuth2 获取访问令牌导致“无效授权” - Calling Google OAuth2 from C# To Get Access Token Results in "Invalid Grant"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM