use*_*495 4 api authorization reactjs .net-core reactjs.net
我们有一个新应用程序,它以 ReactJS 作为前端,后端是 .NET Core API。
要求是针对 Active Directory 授权 Windows 登录用户。
.NET Core API 将负责授权部分。
我们在 .NET Core API 中使用了以下代码,但它返回 .NET Core API 的应用程序池正在运行的 ID。我们尝试将 API 设置为启用 Windows 身份验证,但效果不佳。
dynamic userIdentity = WindowsIdentity.GetCurrent();
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
Run Code Online (Sandbox Code Playgroud)
我已将代码更改为以下内容:
dynamic userIdentity = WindowsIdentity("UserID");
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
Run Code Online (Sandbox Code Playgroud)
我们需要将 UserID从ReactJS 传递到 API 层。
在 ReactJS 中我尝试了以下操作:
var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
Run Code Online (Sandbox Code Playgroud)
但这在 ReactJS 中不起作用。
有没有一种方法可以在React JS中获取Windows登录ID并将其传递给API层进行授权?
我们能够通过以下方法完成此任务:
在IIS下我们托管的网站如下:
添加了一个网站ReactJSWeb。
我。在ReactJS网站下添加.NETCore虚拟目录。
主网站和虚拟目录的身份验证均设置为“启用 Windows 身份验证”。
在.NET Core API - 身份验证模块中,我们在类上添加了属性[Authorize],并在方法中添加了以下代码:
使用 Microsoft.AspNetCore.Authorization;
dynamic userIdentity = WindowsIdentity(**User.Identity.Name**);
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
Run Code Online (Sandbox Code Playgroud)
这很有效,我们现在可以根据用户所属的 Active Directory 安全组正确进行授权。
| 归档时间: |
|
| 查看次数: |
7444 次 |
| 最近记录: |