我正在开发 ASP.NET Core Web 应用程序 (.NET 5.0)。这是一个 Intranet 应用程序,因此我使用 Windows 身份验证。对于授权,我使用 AspNetCore.Identity 中的自定义角色(出于各种原因不想使用 AD 组)。我正在使用该类IClaimsTransformation来实现一个TransformAsync方法,以便将我的自定义角色添加到用户的声明中。我[Authorize(Roles = "Admin")]在控制器上添加了以测试整个方案。
当我在调试(IIS Express)中测试时,该TransformAsync方法永远不会被调用。
我确实检查了我的 IIS 中是否启用了 Windows 身份验证launchSettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:49861",
"sslPort": 44307
}
Run Code Online (Sandbox Code Playgroud)
我还检查了是否为调试模式启用了 windowsAuthentication (IIS Express): 在此处输入图像描述
下面是我的ConfigureServices方法(startup.cs):
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>
(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
// Windows Authentication
services.AddAuthentication(IISDefaults.AuthenticationScheme);
// Claim transformation
services.AddScoped<IClaimsTransformation, AddRolesClaimsTransformation>();
//ASP Identity
services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>(); …Run Code Online (Sandbox Code Playgroud)