如何在core2.2 mvc项目中使用IClaimsTransformation
how to use IClaimsTransformation in core2.2 mvc project
我正在尝试学习如何使用 IClaimsTransformation
修改 windows 身份验证中的用户声明。但是当我尝试使用它时,我收到一条错误消息
"InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found."
我主要在 mac 上尝试,但我也在公司域中的公司电脑上尝试过。他们两个都给了我同样的错误。我也是 IIS express(VS 和 Rider 的调试模式)。
在我的启动文件中
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSingleton<IClaimsTransformation, UserClaims>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment 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.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我有这个 class 用于索赔转换
public class UserClaims: IClaimsTransformation
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var ci = (ClaimsIdentity) principal.Identity;
var c = new Claim(ci.RoleClaimType, "Admin");
ci.AddClaim(c);
return Task.FromResult(principal);
}
}
我的控制器也使用这个装饰器
[Authorize(Roles = "Admin")]
"InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found."
我几个月前使用 Windows 身份验证时遇到了同样的问题。原来我没有启用 Windows Authentication
。
请检查 Properties/Debug 选项卡,并确保 Enable Windows Authentication
已选中:
附带说明一下,您正在修改 原始 principal
。 IMO,返回 全新 主体是首选:
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var cp = principal.Clone(); // create a copy
var ci = (ClaimsIdentity)cp.Identity;
var c = new Claim(ci.RoleClaimType, "Admin");
ci.AddClaim(c); // modify the copy
return Task.FromResult(cp);
}
首先,将 Rider 用作 IDE 搞乱了我的调试设置,在删除演示应用程序并将调试设置恢复为默认的 IIS Express 设置后,我设法让我的代码正常工作。
之后,每次我尝试调试我的应用程序时都会出现 403 错误,在@itminus 的帮助下,我们在我的中间件订单中找到了问题。我在 UseAuthentication() 上使用 UseAuthorization(),这是我的错误。所以将 UseAuthentication() 放在 UseAuthorization() 上解决了我的第二个问题。
我正在尝试学习如何使用 IClaimsTransformation
修改 windows 身份验证中的用户声明。但是当我尝试使用它时,我收到一条错误消息
"InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found."
我主要在 mac 上尝试,但我也在公司域中的公司电脑上尝试过。他们两个都给了我同样的错误。我也是 IIS express(VS 和 Rider 的调试模式)。
在我的启动文件中
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSingleton<IClaimsTransformation, UserClaims>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment 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.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我有这个 class 用于索赔转换
public class UserClaims: IClaimsTransformation
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var ci = (ClaimsIdentity) principal.Identity;
var c = new Claim(ci.RoleClaimType, "Admin");
ci.AddClaim(c);
return Task.FromResult(principal);
}
}
我的控制器也使用这个装饰器
[Authorize(Roles = "Admin")]
"InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found."
我几个月前使用 Windows 身份验证时遇到了同样的问题。原来我没有启用 Windows Authentication
。
请检查 Properties/Debug 选项卡,并确保 Enable Windows Authentication
已选中:
附带说明一下,您正在修改 原始 principal
。 IMO,返回 全新 主体是首选:
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) { var cp = principal.Clone(); // create a copy var ci = (ClaimsIdentity)cp.Identity; var c = new Claim(ci.RoleClaimType, "Admin"); ci.AddClaim(c); // modify the copy return Task.FromResult(cp); }
首先,将 Rider 用作 IDE 搞乱了我的调试设置,在删除演示应用程序并将调试设置恢复为默认的 IIS Express 设置后,我设法让我的代码正常工作。
之后,每次我尝试调试我的应用程序时都会出现 403 错误,在@itminus 的帮助下,我们在我的中间件订单中找到了问题。我在 UseAuthentication() 上使用 UseAuthorization(),这是我的错误。所以将 UseAuthentication() 放在 UseAuthorization() 上解决了我的第二个问题。