-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using AuthSample.ViewModels.Admin; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
|
||
namespace AuthSample.Controllers | ||
{ | ||
[Authorize] | ||
public class AdminController : Controller | ||
{ | ||
private readonly RoleManager<IdentityRole> _roleManager; | ||
|
||
public AdminController(RoleManager<IdentityRole> roleManager) | ||
{ | ||
_roleManager = roleManager; | ||
} | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
[HttpGet] | ||
public IActionResult CreateRole() | ||
{ | ||
return View(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<IActionResult> CreateRoleAsync(CreateRoleViewModel model) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
//角色 Name 不能重覆 | ||
var identityRole = new IdentityRole | ||
{ | ||
Name = model.RoleName, | ||
}; | ||
|
||
IdentityResult result = await _roleManager.CreateAsync(identityRole); | ||
|
||
if (result.Succeeded) | ||
{ | ||
return RedirectToAction("index", "home"); | ||
} | ||
|
||
foreach(IdentityError error in result.Errors) | ||
{ | ||
ModelState.AddModelError("", error.Description); | ||
} | ||
} | ||
return View(model); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace AuthSample.ViewModels.Admin | ||
{ | ||
public class CreateRoleViewModel | ||
{ | ||
[Required] | ||
[Display(Name = "角色")] | ||
public string RoleName { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@using AuthSample.ViewModels.Admin | ||
@* | ||
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | ||
*@ | ||
@{ | ||
ViewBag.Title = "建立角色"; | ||
} | ||
@model CreateRoleViewModel | ||
<form asp-action="CreateRole" method="post"> | ||
<div> | ||
<label asp-for="RoleName"></label> | ||
<input asp-for="RoleName" /> | ||
<span asp-validation-for="RoleName"></span> | ||
</div> | ||
<div> | ||
<button>創建角色</button> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters