gpt4 book ai didi

reactjs - ReactJS 和 .NET Core API 中的 windows 授权

转载 作者:行者123 更新时间:2023-12-03 13:44:12 25 4
gpt4 key购买 nike

我们有一个新应用程序,它以 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);

我已将代码更改为以下内容:

            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 下,我们托管网站如下:

  1. 添加了网站 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);

这很有效,我们现在可以根据用户所属的 Active Directory 安全组正确进行授权。

关于reactjs - ReactJS 和 .NET Core API 中的 windows 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com