我认为标题可能有些歪斜,但是,这是我的问题和目标。我正在使用ASP.NET Core 3.1 MVC开发应用程序。我需要限制用户对某些区域,页面等的访问。这已经在Startup.cs
文件中完成,并将[Authorize]
属性添加到管理控制器中。但是,我似乎无法弄清楚的是:如果管理员在该用户登录时删除了该用户的管理权限,并且他们试图访问受保护的页面,那么如何阻止他们访问该页面?我知道逻辑答案可能是让用户注销并重新登录,但是,在这种情况下,这不是必需的。
文件Startup.cs
(代码段)
public void ConfigureServices(IServiceCollection services)
{
//Configuration
services.Configure<HHConfig>(Configuration.GetSection("App"));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequiredUniqueChars = 1;
options.SignIn.RequireConfirmedAccount = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddControllersWithViews();
services.AddRazorPages(options => {
options.Conventions.AuthorizeFolder("/Administration");
});
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/Account/Login");
});
//Transient and Scoped Services Here
services.AddTransient<ApplicationDbContext>();
services.AddScoped<IEmailManager, EmailManager>();
}
行政主管
[Authorize(Roles = "Admin")]
public class AdministrationController : Controller
{
private readonly RoleManager<IdentityRole> roleManager;
private readonly UserManager<ApplicationUser> userManager;
private SignInManager<ApplicationUser> signInManager { get; }
private readonly IEmailManager emailManager;
public AdministrationController(RoleManager<IdentityRole> roleManager,UserManager<ApplicationUser> userManager,SignInManager<ApplicationUser> signInManager, IEmailManager emailMgr)
{
this.roleManager = roleManager;
this.userManager = userManager;
this.signInManager = signInManager;
emailManager = emailMgr;
}
}
您遇到的问题是用户的声明存储在用户浏览器的cookie中。您需要做的是确保在更新用户声明时更新cookie。一种方法是您可以刷新关键页面上的用户登录。
这可以通过以下方法完成:
注册一个,UserClaimsPrincipalFactory
以便每次SignInManager
唱歌时都会创建声明。
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, UserClaimService>();
实施UserClaimsPrincipalFactory<TUser, TRole>
如下的自定义
public class UserClaimService : UserClaimsPrincipalFactory<ApplicationUser, ApplicationRole>
{
private readonly ApplicationDbContext _dbContext;
public UserClaimService(ApplicationDbContext dbContext, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager, IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
{
_dbContext = dbContext;
}
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
// Get user claims from DB using dbContext
// Add claims
((ClaimsIdentity)principal.Identity).AddClaim(new Claim("claimType", "some important claim value"));
return principal;
}
}
在您的应用程序的后面,当您更改数据库中的某些内容并希望将其反映给您的已验证并登录的用户时,以下几行代码可以实现此目的:
var user = await _userManager.GetUserAsync(User);
await _signInManager.RefreshSignInAsync(user);
这将使用户安静地再次登录,而无需他们进行任何交互,并确保向他们显示最新的内容。尽管这不是一个相同的问题,但是在这个问题上还有其他答案可以帮助解决这个问题。我必须在哪里为这个答案功劳。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句