Разграничил доступы к ресурсам посредством ролей

parent 04706103
...@@ -55,5 +55,11 @@ namespace webinar65.Controllers ...@@ -55,5 +55,11 @@ namespace webinar65.Controllers
return RedirectToAction("Login"); return RedirectToAction("Login");
} }
public IActionResult AccessDenied(string returnUrl)
{
ViewBag.returnUrl = returnUrl;
return View();
}
} }
} }
...@@ -5,7 +5,7 @@ using webinar65.Models; ...@@ -5,7 +5,7 @@ using webinar65.Models;
namespace webinar65.Controllers namespace webinar65.Controllers
{ {
[Authorize] [Authorize(Roles = "Users,Admins")]
public class HomeController : Controller public class HomeController : Controller
{ {
public IActionResult Index() public IActionResult Index()
......
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using webinar65.ViewModels; using webinar65.ViewModels;
namespace webinar65.Controllers namespace webinar65.Controllers
{ {
[Authorize(Roles = "Admins")]
public class RoleController : Controller public class RoleController : Controller
{ {
private RoleManager<IdentityRole> _roleManager; private RoleManager<IdentityRole> _roleManager;
......
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using webinar65.Models; using webinar65.Models;
...@@ -7,6 +8,7 @@ using webinar65.ViewModels; ...@@ -7,6 +8,7 @@ using webinar65.ViewModels;
namespace webinar65.Controllers namespace webinar65.Controllers
{ {
[Authorize(Roles = "Admins")]
public class UserController : Controller public class UserController : Controller
{ {
private UserManager<User> _userManager; private UserManager<User> _userManager;
...@@ -44,7 +46,10 @@ namespace webinar65.Controllers ...@@ -44,7 +46,10 @@ namespace webinar65.Controllers
var result = await _userManager.CreateAsync(newUser, model.Password); var result = await _userManager.CreateAsync(newUser, model.Password);
if (result.Succeeded) if (result.Succeeded)
{
await _userManager.AddToRoleAsync(newUser, model.RoleName);
return RedirectToAction("Index"); return RedirectToAction("Index");
}
foreach (var error in result.Errors) foreach (var error in result.Errors)
{ {
......
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Forbidden</title>
</head>
<body>
<div>
<h1>Access Denied</h1>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment