Create a new Adminviewmodel file, create a view model class
public class Roleviewmodel
{
public string Id {get; set;}
[Required (Allowemptystrings=false)]
[Display (name= "role name")]
public string Name {get; set;}
[Display (name= "role description")]
[Stringlength (50,errormessage= "{0} cannot exceed 50 characters")]
[DataType (Datatype.multilinetext)]
public string Description {get; set;}
}
Create a new role controller Roleadmincontroller and initialize the Usermanager value.
public class Roleadmincontroller:controller
{
Public Roleadmincontroller ()
{ }
constructor injection;
Public Roleadmincontroller (Applicationusermanager Usermanager, Applicationrolemanager rolemanager)
{
Usermanager = Usermanager;
rolemanager = rolemanager;
}
Defining fields
Private Applicationusermanager _usermanager;
Public Applicationusermanager Usermanager
{
Get
{
Return _usermanager?? Httpcontext.getowincontext (). Getusermanager<applicationusermanager> ();//microsoft.aspnet.identity.owin namespace. Null-value merge operator.
}
Set
{
_usermanager = value;
}
}
Private Applicationrolemanager _rolemanager;
Public Applicationrolemanager rolemanager
{
Get
{
Return _rolemanager?? Httpcontext.getowincontext (). Get<applicationrolemanager> ();
}
Set
{
_rolemanager = value;
}
}
List of roles to list the users of each role in the role home page
Model:
public class Indexroleviewmodel
{
public string Id {get; set;}
[Required (allowemptystrings = False)]
[Display (name = "role name")]
public string Name {get; set;}
[Display (Name = "role description")]
[Stringlength (errormessage = "{0} cannot exceed 50 characters")]
[DataType (Datatype.multilinetext)]
public string Description {get; set;}
Public list<applicationuser> applicationusers {get; set;}
}
}
Controller
Get:admin/roleadmin
Public async task<actionresult> Index ()
{
var roleslist =new list<indexroleviewmodel> ();
foreach (var role in await RoleManager.Roles.ToListAsync ())
{
var _indexroleviewmodel = new Indexroleviewmodel ()
{
Id = role. Id
Name = role. Name,
Description = role. Description,
Applicationusers =new list<applicationuser> ()//special note, initialize an empty List generic collection. If not initialized, an exception with a reference of NULL is triggered.
};
foreach (var user in await UserManager.Users.ToListAsync ())
{
if (usermanager.isinrole (user. Id,role. Name)//traverse the user, and if the user has this role, the user is added to the collection.
{
_indexroleviewmodel.applicationusers.add (user);
}
}
Roleslist.add (_indexroleviewmodel); Finally, this viewmodel is added to the list and returned to the view.
}
Return View (Roleslist.tolist ()); The Async method is under the System.Data.Entity namespace.
}
View:
@model ienumerable<majorconstruction.areas.admin.models.indexroleviewmodel>
@{
Viewbag.title = "Role list";
}
<table class= "Table table-striped table-hover" >
<thead>
<tr>
<th>
@Html. DisplayName ("Role name")
</th>
<th>
@Html. displaynamefor (model = model. Description)
</th>
<th> @Html. DisplayName ("Members with this role") </th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html. displayfor (ModelItem = Item. Name)
</td>
<td>
@Html. displayfor (ModelItem = Item. Description)
</td>
<td>
<ol>
@foreach (Var userforrole in item. Applicationusers)
{
<li> @userForRole. UserName | @userForRole .realname</li>
}
</ol>
</td>
<td>
@Html. ActionLink ("Editing", "edit", new {id = Item). Id}) |
@Html. ActionLink ("verbose", "details", new {id = Item). ID})
</td>
</tr>
}
</tbody>
</table>
Role details
Controller:
Get:admin/roleadmin/details/5
Public Async task<actionresult> Details (string id)
{
if (id = = NULL)
{
return new Httpstatuscoderesult (httpstatuscode.badrequest);
}
var role = await rolemanager.findbyidasync (ID);
if (role = = null)
{
return Httpnotfound ();
}
var users = new list<applicationuser> (); First, you define a list generic container to hold the user.
foreach (var user in UserManager.Users.ToList ())
{
if (await Usermanager.isinroleasync (user. Id,role. Name))
{
Users. ADD (user);
}
}
Viewbag.users = Users; If it's just a collection, instead of using ViewModel, you can use the ViewBag dynamic object to bring the data into view.
Viewbag.usercount = users. Count ();
return View (role);
}
View:
@model MajorConstruction.Areas.Admin.Models.ApplicationRole
@{
Viewbag.title = "role Details";
}
<div>
<DL class= "Dl-horizontal" >
<dt>
Role name
</dt>
<dd>
@Html. displayfor (model = model. Name)
</dd>
<dt>
@Html. displaynamefor (model = model. Description)
</dt>
<dd>
@Html. displayfor (model = model. Description)
</dd>
</dl>
</div>
@if (Viewbag.usercount = = 0)
{
<p> no users have this role </p>
}
<table class= "Table table-striped table-hover" >
<tr>
<th> User name </th>
<th> name </th>
<th> Sex </th>
<th> Email </th>
</tr>
@foreach (var item in viewbag.users)
{
<tr>
<td> @item. Username</td>
<td> @item. Realname</td>
<td> @item. Gender</td>
<td> @item. Email</td>
</tr>
}
</table>
<p>
@Html. ActionLink ("editor", "edit", new {id = model.id}) |
@Html. ActionLink ("Return to Role List", "Index")
</p>
Edit roles: You can use the T4 template to quickly build, no special place.
Controller:
Public Async task<actionresult> Edit (string id)
{
if (id = = NULL)
{
return new Httpstatuscoderesult (httpstatuscode.badrequest);
}
var role = await rolemanager.findbyidasync (ID);
if (role = = null)
{
return Httpnotfound ();
}
Roleviewmodel Roleviewmodel = new Roleviewmodel {Id = role. Id, Name = role. Name, Description = role. Description};
Return View (Roleviewmodel);
}
[HttpPost]
[Validateantiforgerytoken]
Public async task<actionresult> Edit ([Bind (Include = "id,name,description")] Roleviewmodel Rolemodel)
{
if (modelstate.isvalid)
{
var role = await rolemanager.findbyidasync (rolemodel.id);
Role. Name = Rolemodel.name;
Role. Description = rolemodel.description;
await Rolemanager.updateasync (role);
Return redirecttoaction ("Index");
}
Return View (Rolemodel);
}
View:
@model MajorConstruction.Areas.Admin.Models.RoleViewModel
@{
Viewbag.title = "Edit Role";
}
@using (Html.BeginForm ())
{
@Html. AntiForgeryToken ()
<div class= "Form-horizontal" >
@Html. ValidationSummary (True, "", new {@class = "Text-danger"})
@Html. hiddenfor (model = model. ID)
@Html. Hiddenfor (Model =>model. Name)
<div class= "Form-group" >
@Html. labelfor (model = model. Name, htmlattributes:new {@class = "Control-label col-md-2"})
<div class= "Col-md-10" >
<p class= "Form-control-static" > @Html. Displayfor (Model =>model. Name) </p>
</div>
</div>
<div class= "Form-group" >
@Html. labelfor (model = model. Description, htmlattributes:new {@class = "Control-label col-md-2"})
<div class= "Col-md-10" >
@Html. editorfor (model = model. Description, new {htmlattributes = new {@class = "Form-control", rows= "5"}})
@Html. validationmessagefor (model = model. Description, "", new {@class = "Text-danger",})
</div>
</div>
<div class= "Form-group" >
<div class= "Col-md-offset-2 col-md-10" >
<input type= "Submit" value= "Save" class= "btn Btn-default"/>
</div>
</div>
</div>
}
<div>
@Html. ActionLink ("Return to Role List", "Index")
</div>
@section Scripts {
@Scripts. Render ("~/bundles/jqueryval")
}
There is no difference between deleting a role and creating a new role and vs auto-generating, in a general background control management system, the authorization of the permission role is written to die in the program, so it is not necessary to provide the ability to create a new role and remove a role, just initialize it in the role initializer.
However, if you are assigning role permissions dynamically, such as when a role has permission to view an action, there is a need to add and delete functions.
The use of ASP. Identity2 Roles (role) (ii)