简体   繁体   English

将自定义声明从 Angular 应用程序传递到 dotnet core web api

[英]Passing custom claims from Angular app to dotnet core web api

I have a working Angular 9 app which calls a Dotnet core 3.1 web api backend to return data.我有一个有效的 Angular 9 应用程序,它调用 Dotnet 核心 3.1 web api 后端返回数据。 I secure it using the @angular/msal MSAL library which authenticate the AzureAD user on the angular app then sends the AzureADBearer token to the API to access the endpoints.我使用@angular/msal MSAL 库对其进行保护,该库在 angular 应用程序上对 AzureAD 用户进行身份验证,然后将 AzureADBearer 令牌发送到 API 以访问端点。 On angular app, we have an employeeID which we get using Microsoft Graph.在 angular 应用程序上,我们有一个使用 Microsoft Graph 获得的员工 ID。 We wanted to pass this in to the web api in order to use it in some of the controllers.我们想将它传递给 web api 以便在某些控制器中使用它。 I am able to get the claims of the authenticate user from the api by accessing the HttpContextAccessor, i see username, email, sid... etc. my question is, is there anyway to pass in that employeeID so that I can get it in the claims?我可以通过访问 HttpContextAccessor 从 api 获取经过身份验证的用户的声明,我看到了用户名、email、sid ......等等索赔? I'm not sure if that's even possible.我不确定这是否可能。 AzureAD allows optional additional claims to be added in their configuration but employeeID was not on the list. AzureAD 允许在其配置中添加可选的附加声明,但employeeID 不在列表中。 I have 2 workarounds that are not ideal and I would prefer not to do if possible.我有 2 个不理想的解决方法,如果可能的话,我不希望这样做。

  1. passing the employeeID in the controller to the endpoints then accessing it.将 controller 中的员工 ID 传递给端点,然后访问它。 this adds a lot of extract parts to our routes.这为我们的路线增加了很多提取部分。
  2. use a mapping table in our database that maps username to an email id so that we can use the claims to get the username then lookup the employeeID.在我们的数据库中使用映射表,将用户名映射到 email id,以便我们可以使用声明来获取用户名,然后查找员工 ID。 problem is sometimes a user changes their username that and we would have to maintain this mapping table somehow.问题是有时用户更改了他们的用户名,我们必须以某种方式维护这个映射表。 the sid also can change because a user can go on vacation, their access to Azure is deleted. sid 也可以更改,因为用户可以 go 度假,他们对 Azure 的访问权限被删除。 once that user comes back, a new account is created with new sid.一旦该用户回来,就会使用新的 sid 创建一个新帐户。 The employeeID is the only identifier that won't change. employeeID 是唯一不会改变的标识符。

you can use the claims mapping feature, as per here https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-claims-mapping您可以使用声明映射功能,按照此处https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-claims-mapping

this can allow you to emit claims that aren't in the default claims or optional claims.这可以让您发出不在默认声明或可选声明中的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dotnet 核心 Web API 正在从角度形式获取 Null 数据 - Dotnet core web API is getting Null data from angular form CORS 问题 Angular MSAL Azure AD Do.net Core Web API - CORS Issue with Angular MSAL Azure AD Dotnet Core Web API 从 Angular 9 传递到 Web API 的 Z9E0DA8438E1E38A1DDZZCEB4FZ 后,日期变为落后一天 - The Date becomes one day behind after passing from Angular 9 to Web API of ASP.NET Core 具有Microsoft Graph API的Dotnet Core,Angular 5 SPA - Dotnet Core, Angular 5 SPA with Microsoft Graph API dotnet core 便携式 angular 应用程序未运行 - dotnet core portable angular app not running 使用“ dotnet run”命令运行时,Angular / NET Core Web API没有被命中 - Angular/NET Core Web API not getting hit while running using “dotnet run” command Web Api 2中的自定义路由以使用角度应用程序进行调用 - Custom route in Web Api 2 to call with angular app 如何在 signalR 集线器 web api Z2D50972FCECD1761295Z9550F8 中从用户那里获得声明 - How to get Claims from User in signalR hub web api .net core 来自angular 5的dotnet core 2.0身份验证 - dotnet core 2.0 authentication from angular 5 如何将VS Code的.Net Core Web Api和Angular 6应用发布到Azure? - How to publish a .Net Core Web Api and Angular 6 app from VS Code to Azure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM