Witam serdecznie. Jakim cudem mając taki kod:
[Authorize]
public async Task<IActionResult> Index()
{
string[] roleNames = { Roles.Admin, Roles.Customer, Roles.Merchant, Roles.Technician };
foreach (var roleName in roleNames)
{
bool x = await _roleManager.RoleExistsAsync(roleName);
_logger.LogInformation($"role {roleName} = {x}");
if (!x)
{
var role = new IdentityRole();
role.Name = roleName;
await _roleManager.CreateAsync(role);
}
}
var CurrentUser = await _userManager.GetUserAsync(User);
if (CurrentUser == null)
throw new Exception();
switch (CurrentUser.ApplicationUserType)
{
case eApplicationUserType.Admin:
if (User.IsInRole(Roles.Admin))
_logger.LogInformation("User is in role " + Roles.Admin);
else
{
_logger.LogInformation("Adding to role: " + Roles.Admin);
await _userManager.AddToRoleAsync(CurrentUser, Roles.Admin);
}
return View("Index");
default:
return RedirectToAction("Index", "Home");
}
}
otrzymuję od loggera:
Information: role Admin = True
Information: role Customer = True
Information: role Merchant = True
Information: role Technician = True
Information: User is in role Admin
Jeżeli zmienię [Authorize]
na [Authorize(Roles="Admin")]
otrzymuję brak dostępu. Robię to identycznie jak w innej swojej aplikacji. Tam działa, a tu nie :O