Я пытаюсь узнать, как использовать IClaimsTransformation
для изменения утверждений пользователей в Windows-аутентификации. Но когда я пытаюсь использовать его, я получаю сообщение об ошибке:
«InvalidOperationException: не указана схема authenticationScheme, и не найден DefaultChallengeScheme».
Я в основном пробовал это на 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?}");
});
}
, и у меня есть этот класс для преобразования утверждений
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")]