2018教程之mvc+ef之五:Identity角色管理之新建角色
用户管理基本功能完毕,现在我们来看看如何建立角色。
当我们使用ASP.NET 4.5创建模板项目时,会发现模板只提供了ApplicationUserManager用于用户的登录注册、修改、设置等,而没有提供与用户角色相关的代码,对此就需要我们自己手动的添加
一、在IdentityConfig.cs文件里面添加一个与ApplicationUserManager类似的类:
//定义角色管理器
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore) : base(roleStore)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
}
}
二、在Startup.Auth.cs文件里面创建与ApplicationRoleManager相对应的上下文
public partial class Startup
{
//...
// 有关配置身份验证的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// 将数据库上下文和用户管理器配置为对每个请求使用单个实例
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);//添加角色管理器
//...
}
}
三、第三步:可以在AccountController类里面封装成属性来使用:
private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
get { return _roleManager ?? Request.GetOwinContext().Get<ApplicationRoleManager>(); }
set { _roleManager = value; }
}
四、开始我们的新建角色
1.新建个AddRoleViewModel的模型
using System.ComponentModel.DataAnnotations;
namespace MYtest2018.ViewModels
{
public class AddRoleViewModel
{
[Required]
[StringLength(20)]
[Display(Name = "角色名称")]
public string Name { get; set; }
}
}
五、后台
/// <summary>
/// 添加角色
/// </summary>
/// <returns></returns>
[HttpGet]
//[Authorize(Roles = "Super")]
public ActionResult AddRoles()
{
return View();
}
/// <summary>
/// 添加角色
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
//[Authorize(Roles = "Super")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddRoles(AddRoleViewModel model)
{
if (ModelState.IsValid)
{
var oneRole = await RoleManager.FindByNameAsync(model.Name);
if (oneRole == null)
{
var result1 = await RoleManager.CreateAsync(new IdentityRole(model.Name));
if (result1.Succeeded)
{
return RedirectToAction("ListUsers");
}
AddErrors(result1);
}
return View("角色已存在!!!!!");
}
return View();
}
六、前台
@model MYtest2018.ViewModels.AddRoleViewModel
@{
ViewBag.Title = "AddRoles";
Layout = "~/Views/Shared/_Layoutadmin.cshtml";
}
<div class="wrapper wrapper-content">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h2>创建新用户</h2>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="ibox-content">
@using (Html.BeginForm("AddRoles", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="保存" />
</div>
</div>
}
</div>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
七,效果

浙公网安备 33010602011771号