【问题标题】:windows Authorization in ReactJS and .NET Core APIReactJS 和 .NET Core API 中的 windows 授权
【发布时间】:2018-07-03 23:17:56
【问题描述】:

我们有一个新的应用程序,它使用 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 层进行授权?

【问题讨论】:

  • 也许看看helloJS?支持windows认证

标签: reactjs api .net-core authorization reactjs.net


【解决方案1】:

我们能够通过以下方法完成这项工作:

在 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 安全组正确地进行授权。

【讨论】:

  • 第二。这对于阅读更多上下文非常有帮助
  • 第二。对我来说同样的问题
【解决方案2】:

是否可以分享您按照该方法对客户端进行身份验证所做的工作?像你这样的类似场景让我快疯了......

我非常感谢。

【讨论】:

  • 请不要使用答案部分来回答您的问题。你可以随时提问...
  • 对不起!但是我想在这里做一些cmets(或问题),但我没有足够的积分来做那......对不起!
猜你喜欢
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 2020-04-15
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
相关资源
最近更新 更多