根据用户角色显示或隐藏导航菜单
@if (SignInManager.IsSignedIn(User) && User.IsInRole("Admin"))
{
<li class="nav-item">
<a class="nav-link" asp-controller="Admin" asp-action="ListRoles">角色管理</a>
</li>
}
默认的拒绝访问地址是 AccessDenied:http://localhost:3290/Account/AccessDenied?ReturnUrl=%2FAdmin%2FListRoles
[HttpGet]
[AllowAnonymous]
public IActionResult AccessDenied()
{
return View();
}
AccessDenied 页面:
@{
ViewBag.Title = "拒绝访问";
}
<div class="text-center">
<h1 class="text-danger">拒绝访问</h1>
<h6 class="text-danger">您没有查看此资源的权限</h6>
<img src="~/images/noaccess.png" style="width: 300px; height: 300px;" />
</div>
自定义拒绝访问地址
也可以在 ConfigureServices 里面自定义拒绝访问的地址:
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/Account/Fangyu";
});
此时访问拒绝地址就会变成 Fangyu:http://localhost:3290/Account/Fangyu?ReturnUrl=%2FAdmin%2FListRoles