问题描述
我添加了一个管理员角色,应该允许管理员导航到一个页面。导航选项只能由管理员查看。
以下是我如何定义(和添加)管理员角色和用户 (startup.cs
):
//this credentials are NOT for production
private async Task CreateRolesandUsers(RoleManager<IdentityRole> roleManager,UserManager<ApplicationUser> userManager)
{
bool x = await roleManager.RoleExistsAsync("Admin");
if(!x)
{
// first we create Admin rool
var role = new IdentityRole();
role.Name = "Admin";
await roleManager.CreateAsync(role);
//Here we create a Admin super user who will maintain the website
var user = new ApplicationUser();
user.UserName = "aviv@gmail.com";
user.Email = "aviv@gmail.com";
string userPWD = "aviv@gmail.com";
IdentityResult chkUser = await userManager.CreateAsync(user,userPWD);
//Add default User to Role Admin
if(chkUser.Succeeded)
{
var result1 = await userManager.AddToRoleAsync(user,"Admin");
}
}
}
检查了数据库,它似乎更新得很好,添加了用户和角色并将两者连接起来。尝试使用上述凭据登录时,登录工作正常。
然而,在 Blazor WebAssembly 客户端,当我尝试显示管理员角色时 - 只有这样的内容:
<AuthorizeView>
<Authorized>
stuff for any logged in use (works)
</Authorized>
<NotAuthorized></NotAuthorized>
</AuthorizeView>
<AuthorizeView Roles="Admin">
<h1>test2</h1>
<button @onclick="AdminRedirect">Ban users</button>
stuff only for admins (dosen't work)
</AuthorizeView>
然后尝试从我的管理员帐户登录并导航到此页面,我只能看到每个登录用户都可以看到的内容,而不能看到管理员 - 只有内容。
这是为什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)