text stringlengths 0 13.4k |
|---|
84 |
Using identity in the application |
All done! Try using the application with two different user accounts. The |
to-do items stay private for each account. |
85 |
Authorization with roles |
Authorization with roles |
Roles are a common approach to handling authorization and permissions |
in a web application. For example, it's common to create an |
Administrator role that gives admin users more permissions or power |
than normal users. |
In this project, you'll add a Manage Users page that only administrators |
can see. If normal users try to access it, they'll see an error. |
Add a Manage Users page |
First, create a new controller: |
Controllers/ManageUsersController.cs |
using System; |
using System.Linq; |
using System.Threading.Tasks; |
using Microsoft.AspNetCore.Mvc; |
using Microsoft.AspNetCore.Authorization; |
using Microsoft.AspNetCore.Identity; |
using AspNetCoreTodo.Models; |
using Microsoft.EntityFrameworkCore; |
namespace AspNetCoreTodo.Controllers |
{ |
[Authorize(Roles = "Administrator")] |
public class ManageUsersController : Controller |
{ |
private readonly UserManager<ApplicationUser> |
_userManager; |
public ManageUsersController( |
UserManager<ApplicationUser> userManager) |
{ |
_userManager = userManager; |
} |
86 |
Authorization with roles |
public async Task<IActionResult> Index() |
{ |
var admins = (await _userManager |
.GetUsersInRoleAsync("Administrator")) |
.ToArray(); |
var everyone = await _userManager.Users |
.ToArrayAsync(); |
var model = new ManageUsersViewModel |
{ |
Administrators = admins, |
Everyone = everyone |
}; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.