【问题标题】:How to manipulate Claims in ASP.NET Core 5 with Windows authentication如何使用 Windows 身份验证在 ASP.NET Core 5 中操作声明
【发布时间】:2023-03-17 16:16:01
【问题描述】:

我正在研究将 Windows 身份验证与 ASP.NET Core 5.0 MVC 结合使用。我可以直接在 Authorize 属性中使用 Active Directory 组名,但过去证明这是有问题的。

我想将声明添加到列表中并在Authorize 属性中使用这些值。各种帖子建议如果我实现IClaimsTransformationTransformAsync() 将被自动调用,但似乎并非如此。我从未在 Visual Studio 的输出中看到字符串 In TransformAsync

我正在使用 VS 2019 和 ASP.NET Core 5.0。

重新创建:我使用新的 ASP.NET Core Web 应用程序(模型-视图-控制器)模板:

  • 目标框架:.NET 5.0(当前)
  • 身份验证类型:Windows,
  • HTTPS 配置:是

当我运行它时,我可以看到我通过 Hello DOMAIN\User 消息进行了身份验证。

这些是我的补充:

ADClaimsTransformation.cs:

using Microsoft.AspNetCore.Authentication;
using System.Diagnostics;
using System.Security.Claims;
using System.Threading.Tasks;

namespace SOWinAuthN1.Services
{
    public class ADClaimsTransformation : IClaimsTransformation
    {
        public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
        {
            Debug.WriteLine("In TransformAsync");
            var ci = (ClaimsIdentity)principal.Identity;
            var c = new Claim(ci.RoleClaimType, "Admin");
            ci.AddClaim(c);

            return Task.FromResult(principal);
        }
    }
}

我可以从输出窗口中看到它从未被调用过。

Startup.cs,我有:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SOWinAuthN1.Services;

namespace SOWinAuthN1
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddTransient<IClaimsTransformation, ADClaimsTransformation>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

SecureController.cs:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace SOWinAuthN1.Controllers
{
    [Authorize(Roles = "Admin")]
    public class SecureController : Controller
    {
        public IActionResult Index()
        {
            return View(HttpContext.User.Claims);
        }
    }
}

Index.cshtml(位于.\Views\Secure):

@model IEnumerable<System.Security.Claims.Claim>

@{
    ViewData["Title"] = "Claims";
}

<h1>Claims</h1>

<table class="table">
    <thead>
        <tr>
            <th>Subject</th>
            <th>Type</th>
            <th>Value</th>
            <th>Issuer</th>
            <th>OriginalIssuer</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
        <tr>
            <td>@item.Subject</td>
            <td>@item.Type</td>
            <td>@item.Value</td>
            <td>@item.Issuer</td>
            <td>@item.OriginalIssuer</td>
        </tr>
        }
    </tbody>
</table>

_Layout.cshtml - 添加到导航栏ul:

<li class="nav-item">
    <a class="nav-link text-dark" asp-area="" asp-controller="Secure" asp-action="Index">Claims</a>
</li>

我是不是找错树了?有更好的方法吗?

最终,我希望有一个转换器,它将少量的 AD 组声明映射到更多的操作,例如 A 组映射到操作 1、2 和 4,而 B 组映射到 2、4和 5,比如说,计划将此映射保留在某个配置中。

【问题讨论】:

  • 您是如何设置身份验证服务和中间件的?也包括那些在帖子中
  • 我已经包含了整个 Startup.cs - 这是否满足您的要求?核心对我来说是新的,所以我还在寻找自己的脚。

标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity asp.net-core-5.0


【解决方案1】:

您的ConfigureServices 方法中似乎缺少services.AddAuthentication(),而您的Configure 方法中缺少app.UseAuthentication()

您可以查看Configure Windows Authentication in ASP.NET Core,了解有关如何配置 Kestrel 或 IIS 或 HTTP.sys 的更多信息。

【讨论】:

  • 身份验证在没有它们的情况下工作,因为_Layout.cshtml 中的&lt;p class="nav navbar-text"&gt;Hello, @User.Identity.Name!&lt;/p&gt; 被正确呈现为Hello, MYDOMAIN\MyUserId!,如果我注释掉[Authorize] 属性,安全页面会显示我的AD 组成员身份。
  • 我尝试添加这两个语句,但没有任何区别。
  • @user2871239 这是正确答案。
【解决方案2】:

按照前面的说明进行操作,但 在app.UseAuthorization() 之前添加app.UseAuthentication() 很重要

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 2020-10-09
    • 2021-05-31
    • 2016-09-11
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多