Authorization works with roles properly. Added authorization examples to Sample project.
This commit is contained in:
@@ -6,14 +6,19 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Sample.Models;
|
||||
using Raven.Client;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Sample.Controllers
|
||||
{
|
||||
public class HomeController : RavenController
|
||||
{
|
||||
public HomeController(IAsyncDocumentSession dbSession)
|
||||
private UserManager<AppUser> userManager;
|
||||
|
||||
public HomeController(IAsyncDocumentSession dbSession, UserManager<AppUser> userManager)
|
||||
: base(dbSession)
|
||||
{
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
@@ -27,6 +32,20 @@ namespace Sample.Controllers
|
||||
return View();
|
||||
}
|
||||
|
||||
// Require that the user be logged in.
|
||||
[Authorize]
|
||||
public IActionResult Auth()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// Require that the user be in the Admin role.
|
||||
[Authorize(Roles = "Admin")]
|
||||
public IActionResult AuthAdmin()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult About()
|
||||
{
|
||||
ViewData["Message"] = "Your application description page.";
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
|
||||
<PackageReference Include="RavenDB.Identity" Version="2.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -18,4 +17,8 @@
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RavenDB.Identity\RavenDB.Identity.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -32,6 +32,8 @@ namespace Sample
|
||||
.AddRavenDbAsyncSession() // Create a RavenDB IAsyncDocumentSession for each request.
|
||||
.AddRavenDbIdentity<AppUser>(); // Use Raven for users and roles.
|
||||
|
||||
// You can change the login path if need be.
|
||||
// services.ConfigureApplicationCookie(options => options.LoginPath = "/my/login/path");
|
||||
|
||||
// Add application services.
|
||||
services.AddTransient<IEmailSender, EmailSender>();
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Hi, this is a page you can access only if you're signed in.</h1>
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Hi, this is a page you can access only if you're an admin.</h1>
|
||||
Reference in New Issue
Block a user