我卡上,我想在一个asp.net mvc的核心应用提供了解决方案。我想提供一个Web应用程序的解决方案,以标准的用户,角色,权限利用基于新的索赔方式。
我一直在这里跟随福斯特的逻辑(http://benfoster.io/blog/asp-net-identity-role-claims)。在下面(演示质量)的代码,我说明我的方法,我会评论,以帮助显示我的快速和肮脏的测试解决方案。
我所面临的挑战是,它不工作。
//注:我发现了错误,并会评论我哪里错了未来用户寻找一个类似的解决方案。
种子类:这是一个快速和肮脏的解决方案有两个新用户,两个角色和角色的一个部分索赔种子数据库。我这样做是因为它是一个测试应用程序来学习的权利要求的方法来为我的应用程序权限管理。我的完整的解决方案将为每个租户经由UI,联想1或许多索赔的角色(一个或多个)创建自己的角色,然后将角色分配给用户的方式。我想提供一种方式,租户管理自己的用户,以及他们能或不能做什么。这是一个简单的实现了基于声明的办法,如要求具有比1多了很多力量:与策略1的关系。
public class DbInitializer
{
private ApplicationDbContext _context;
private RoleManager<ApplicationRole> _roleManager;
private UserManager<ApplicationUser> _userManager;
public DbInitializer(ApplicationDbContext context,RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager)
{
_roleManager = roleManager;
_userManager = userManager;
_context = context;
}
public async Task Initialize()
{
//RoleManager<IdentityRole> roleManager = new RoleManager<IdentityRole>();
//UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>();
_context.Database.EnsureCreated();
// Look for any students.
if (!_context.Users.Any())
{
//create user and admin role
ApplicationUser adminUser = new ApplicationUser();
adminUser.Email = "[email protected]";
adminUser.UserName = "Admin";
var result = await _userManager.CreateAsync(adminUser, "Password-1");
var newAdminUser = await _userManager.FindByEmailAsync(adminUser.Email);
ApplicationRole adminRole = new ApplicationRole();
adminRole.Name = "Admin";
adminRole.Description = "This is the admin role.";
await _roleManager.CreateAsync(adminRole);
await _roleManager.AddClaimAsync(adminRole, new Claim("Can add roles", "add.role"));
await _roleManager.AddClaimAsync(adminRole, new Claim("Can delete roles", "delete.role"));
await _roleManager.AddClaimAsync(adminRole, new Claim("Can edit roles", "edit.role"));
await _userManager.AddToRoleAsync(newAdminUser, adminRole.Name);
//create user and basic role
ApplicationUser basicUser = new ApplicationUser();
basicUser.Email = "[email protected]";
basicUser.UserName = "Basic";
var resultBasic = await _userManager.CreateAsync(basicUser, "Password-1");
var newBasicUser = await _userManager.FindByEmailAsync(basicUser.Email);
ApplicationRole basicRole = new ApplicationRole();
basicRole.Name = "Basic";
basicRole.Description = "This is the basic role.";
await _roleManager.CreateAsync(basicRole);
//await _roleManager.AddClaimAsync(basicRole, new Claim("Can add roles", "add.role"));
//await _roleManager.AddClaimAsync(basicRole, new Claim("Can delete roles", "delete.role"));
//await _roleManager.AddClaimAsync(basicRole, new Claim("Can edit roles", "edit.role"));
await _userManager.AddToRoleAsync(newBasicUser, basicRole.Name);
await _context.SaveChangesAsync();
}
}
}
}
Startup.CS:创建我的用户,角色和权利要求书(和它们相关联)之后,我需要注册在Startup.cs类Confirgure服务法“政策”。这让我对权利要求的一个或多个策略映射。
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthorization(options =>
{
options.AddPolicy("Add Role",
policy => policy.RequireClaim("Can add roles", "add.role"));
options.AddPolicy("Edit Role",
policy => policy.RequireClaim("Can edit roles", "edit.role"));
options.AddPolicy("Delete Role",
policy => policy.RequireClaim("Can delete roles", "delete.role"));
});
services.AddMvc();
services.AddTransient<DbInitializer>();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
查看:在我的使用情况下,我想从没有与他们被分配到该角色关联的“可以添加角色”要求所有用户限制“添加角色”按钮。视图代码的其余部分是不相关的。我遇到的问题是,我在索赔名传递给AuthorizationService.AuthorizeAsync作为第二个参数VS具有与之相关的要求的“政策”的名字。我因为低于修正它。
@model IEnumerable<ApplicationRoleListViewModel>
@using HailMarry.Models
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
<br />
<div class="top-buffer"></div>
<div class="panel panel-primary">
<div class="panel-heading panel-head">Application Roles</div>
<div class="panel-body">
<div class="btn-group">
//Mistake
//@if (await AuthorizationService.AuthorizeAsync(User, "Can add roles"))
//Fix
@if (await AuthorizationService.AuthorizeAsync(User, "Add Role"))
{
<a id="createRoleButton" asp-action="AddRole" asp-controller="ApplicationRole" class="btn btn-primary">
<i class="glyphicon glyphicon-plus"></i> Add Role
</a>
}
....
最终结果是:我被分配到一个角色“管理”其中有一个要求“可以添加角色”用户“[email protected]”。一个角色可以有任意数量的索赔。我创建了具有相同的要求“可以添加角色”,我在通过注射IAuthorizationService AuthorizationService查看检查策略。如果用户没有这个要求分配给自己的角色,那么政策支票返回true或false将不显示按钮来添加角色。此相同的政策检查逻辑可以被添加到一个控制器,或通过DI感谢任何其他资源,以新的.NET的核心DI中间件。通过这种整个演习,我学会了身份3的强大功能,它可以利用诸如业务逻辑检查。非常甜蜜的东西,虽然作家那里真的需要更多的例子来帮助我们的肉快。总之,希望这有助于未来开发者寻找一个类似的解决方案。
我发现这个问题,我引用的要求“名称” VS在视图中的政策名称...
我会添加注释上述说明的错误,并显示出我在做什么。非常强大的东西,感谢Ben和ASP.Net超过4.5改进的授权解决方案。