windows ReactJS 和 .NET Core 中的授权 API
windows Authorization in ReactJS and .NET Core API
我们有一个新应用程序,它以 ReactJS 作为前端,后端是 .NET Core API。
一项要求是授权 windows 登录用户使用 Active Directory。
.NET Core API 将执行授权部分。
我们在 .NET Core API 中使用了以下代码,但它返回的是 .NET Core API 的 App-Pool 为 运行 的 ID。我们尝试将 API 设置为 Windows 身份验证已启用,但效果不佳。
dynamic userIdentity = WindowsIdentity.GetCurrent();
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
我已将代码更改为以下内容:
dynamic userIdentity = WindowsIdentity("UserID");
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
我们需要将 UserID 从 ReactJS 传递到 API 层。
在 ReactJS 中,我尝试了以下方法:
var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
但这在 ReactJS 中不起作用。
有没有办法在 React JS 中获取 Windows 登录 ID 并将其传递给 API 层进行授权?
我们能够通过以下方法完成此操作:
在 IIS 下我们托管了如下网站:
添加了网站 ReactJSWeb。
我。在ReactJS官网下新增.NETCore虚拟目录
主网站和虚拟目录都将身份验证设置为 Windows 身份验证已启用。
在 .NET Core API - 身份验证模块中,我们在 class 上添加了一个属性 [Authorize] 并在方法中添加了以下代码:
使用 Microsoft.AspNetCore.Authorization;
dynamic userIdentity = WindowsIdentity(**User.Identity.Name**);
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
这有效,我们现在可以根据用户所属的 Active Directory 安全组正确执行授权。
是否可以分享您按照该方法对客户端进行身份验证所做的工作?我对像你这样的类似情况感到疯狂...
非常感谢。
我们有一个新应用程序,它以 ReactJS 作为前端,后端是 .NET Core API。
一项要求是授权 windows 登录用户使用 Active Directory。
.NET Core API 将执行授权部分。
我们在 .NET Core API 中使用了以下代码,但它返回的是 .NET Core API 的 App-Pool 为 运行 的 ID。我们尝试将 API 设置为 Windows 身份验证已启用,但效果不佳。
dynamic userIdentity = WindowsIdentity.GetCurrent();
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
我已将代码更改为以下内容:
dynamic userIdentity = WindowsIdentity("UserID");
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
我们需要将 UserID 从 ReactJS 传递到 API 层。
在 ReactJS 中,我尝试了以下方法:
var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
但这在 ReactJS 中不起作用。
有没有办法在 React JS 中获取 Windows 登录 ID 并将其传递给 API 层进行授权?
我们能够通过以下方法完成此操作:
在 IIS 下我们托管了如下网站:
添加了网站 ReactJSWeb。
我。在ReactJS官网下新增.NETCore虚拟目录
主网站和虚拟目录都将身份验证设置为 Windows 身份验证已启用。
在 .NET Core API - 身份验证模块中,我们在 class 上添加了一个属性 [Authorize] 并在方法中添加了以下代码:
使用 Microsoft.AspNetCore.Authorization;
dynamic userIdentity = WindowsIdentity(**User.Identity.Name**);
dynamic userPrincipal = new WindowsPrincipal(userIdentity);
string Admin = _configuration["AppUserRoles:Admin"];
result = userPrincipal.IsInRole(Admin);
这有效,我们现在可以根据用户所属的 Active Directory 安全组正确执行授权。
是否可以分享您按照该方法对客户端进行身份验证所做的工作?我对像你这样的类似情况感到疯狂...
非常感谢。