Я использую windows аутентификацию и преобразование утверждений с asp. net core 3.1. Я добавляю роли в преобразование претензий. Когда я тестирую User.IsInRole, условие проходит. Проблема в том, что атрибут Authorize не работает. Я попытался создать политику для проверки заявки или роли, но это также не удалось.
Действия контроллера
[AllowAnonymous]
public IActionResult Index()
{
var t = User;
if (User.IsInRole("app"))
{
return RedirectToAction("Banner");
}
return RedirectToAction("Registration");
}
[AllowAnonymous]
public IActionResult Registration()
{
return View();
}
[Authorize(Roles = "app")]
// Does not pass this Auth attribute
public IActionResult Banner()
{
return View();
}
Запуск
services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
services.AddScoped<IClaimsTransformation, ClaimsTransformer>();
services.AddAuthorization(options =>
options.AddPolicy("AppUser", policy => policy.RequireAssertion(context =>
context.User.IsInRole("app")))
);
преобразование заявок
```
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var identity = (ClaimsIdentity)principal.Identity;
var claimsIdentity = new ClaimsIdentity(
identity.Claims,
identity.AuthenticationType,
identity.NameClaimType,
identity.RoleClaimType);
var dbuser = _context.Users.FirstOrDefault(f => f.DoDId == identity.Name);
if (dbuser != null && dbuser.SystemStatus == SystemStatus.Active)
{
claimsIdentity.AddClaim(new Claim(LocalClaimTypes.UserId.ToString(), dbuser.Id.ToString()));
claimsIdentity.AddClaim(new Claim(identity.RoleClaimType, "app"));
var preferenceTheme = _context.Preferences.FirstOrDefault(f => f.ApplicationUserId == dbuser.Id && f.Key == PreferenceKey.Theme);
claimsIdentity.AddClaim(new Claim(LocalClaimTypes.Theme.ToString(), preferenceTheme == null ? "black" : preferenceTheme.Value));
}
var newprinc = new ClaimsPrincipal(claimsIdentity);
return Task.FromResult(newprinc);
}