Loi*_*ger 5 asp.net-identity asp.net-core
我正在开发 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)
这是我的Configure方法(startup.cs):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Event}/{action=Dashboard}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
这是TransformAsync方法:
public class AddRolesClaimsTransformation : IClaimsTransformation
{
private readonly UserManager<ApplicationUser> _userManager;
public AddRolesClaimsTransformation(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
//code removed for simplicity - this never gets called
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试按照这篇文章services.AddAuthentication(IISDefaults.AuthenticationScheme)中的建议更改顺序,但这并没有解决问题。startup.cs
我缺少什么?
我终于找到了自己问题的解决方案。解决方案是更改方法 ( )services.AddAuthentication()中的调用,如下所示:ConfigureServicesstartup.cs
前:
services.AddAuthentication(IISDefaults.AuthenticationScheme);
Run Code Online (Sandbox Code Playgroud)
后:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IISDefaults.AuthenticationScheme;
});
Run Code Online (Sandbox Code Playgroud)
ConfigureServices这是方法 ( )的完整代码startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>
(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
//ASP Identity
services.AddIdentity<ApplicationUser, IdentityRole>
().AddEntityFrameworkStores<ApplicationDbContext>();
// Claim transformation
services.AddScoped<IClaimsTransformation, AddRolesClaimsTransformation>();
// Windows Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IISDefaults.AuthenticationScheme;
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6879 次 |
| 最近记录: |