2018教程之mvc+ef之六:identity创建install安装页面创建默认的管理员
因为项目使用identity,mvc ef code first,会一开始就创好数据库,同时会在后台很多管理使用Super权限,所以我想在项目运行的时候,先创建一个Super超级管理用户。
1.要先创建部门“admin”
2.创建用户:随你输入创建
3.创建角色
4.为用户指定角色
一、在Account中新建install 方法,并创建视图
///在这里准备建一个,install时安装用户,并设为Super /// /// <summary> /// ADD用户,GET方法 /// </summary> /// <returns></returns> [HttpGet] //[Authorize(Roles = "Super")] public ActionResult Install() { ///不读取任务参数 return View(); } /// <summary> /// ADD用户的POST方法 /// </summary> /// <param name="addUserModel"></param> /// <returns></returns> [HttpPost] //[Authorize(Roles = "Super")] [ValidateAntiForgeryToken] public async Task<ActionResult> Install(RegisterViewModel model) { ///创建用户 if (ModelState.IsValid) { var _creatTime = DateTime.Now; var _headerPic = "/Content/noheaderpic.png"; var _BirthDate = DateTime.Now; var _Gender = 1; var _derpartment = "one"; var user = new ApplicationUser { UserName = model.UserName, RealName = model.RealName, Email = model.Email, QQ = model.QQ, DepartmentId = _derpartment, PhoneNumber = model.PhoneNumber, CreateTime = _creatTime, HeaderPic = _headerPic, BirthDate = _BirthDate, Gender = _Gender }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // // 发送包含此链接的电子邮件 // // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户"); ///创建admin部门 Department derpartment = new Department(); derpartment.Id = "one"; derpartment.DepartmentName = "Admin"; derpartment.Sort = 0; db.Departments.Add(derpartment); await db.SaveChangesAsync(); ///创建角色Super AddRoleViewModel addRoleViewModel = new AddRoleViewModel(); addRoleViewModel.Name = "Super"; var result1 = await RoleManager.CreateAsync(new IdentityRole(addRoleViewModel.Name)); ///将用户添加Super的权限 UserManager.AddToRole(user.Id, "Super"); return RedirectToAction("ListUsers", "Account"); } AddErrors(result); } return View(model); }
二,前台
@model MYtest2018.Models.RegisterViewModel @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>INSPINIA | List</title> <link href="~/Content/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="container"> <h2></h2> <div class="row"> 请注意这是第一次安装项目创建初始的Super用户使用,用完请删除install文件,同时默认你创建的用户为Super角色,部门为Admin. </div> <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("Install", "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.UserName, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.RealName, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.RealName, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.PhoneNumber, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.QQ, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.QQ, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) </div> </div> <div class="form-group"> @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.PasswordFor(m => m.ConfirmPassword, 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> </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } <script src="~/Scripts/jquery-3.1.1.min.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> </body> </html>
三、效果。
完成创建后,会转到用户列表
版权声明:本文为xiaodeng1979原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。