-
Notifications
You must be signed in to change notification settings - Fork 162
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
13 changed files
with
257 additions
and
6 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInAuthorize.cshtml
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,15 @@ | ||
@page | ||
@model Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn.LoggedInAuthorizeModel | ||
@{ | ||
ViewData["Title"] = "AuthBuiltIn.LoggedInConfigure"; | ||
} | ||
|
||
<h3>AuthBuiltIn.LoggedInAuthorize</h3> | ||
<p> | ||
If you add <code>[Authorize]</code> to the PageModel in e.g., | ||
</p> | ||
<pre><code> | ||
[Authorize] | ||
public class LoggedInAuthorizeModel : PageModel | ||
{... | ||
</code></pre> |
18 changes: 18 additions & 0 deletions
18
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInAuthorize.cshtml.cs
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 System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn | ||
{ | ||
[Authorize] | ||
public class LoggedInAuthorizeModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInConfigure.cshtml
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,21 @@ | ||
@page | ||
@model Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn.LoggedInConfigureModel | ||
@{ | ||
ViewData["Title"] = "AuthBuiltIn.LoggedInConfigure"; | ||
} | ||
|
||
<h3>AuthBuiltIn.LoggedInConfigure</h3> | ||
<p> | ||
You can configure a razor page in the <code>Startup</code> to logged in e.g., | ||
</p> | ||
<pre><code> | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
//... other code left out | ||
services.AddRazorPages(options => | ||
{ | ||
options.Conventions.AuthorizePage("/AuthBuiltIn/LoggedInConfigure"); | ||
}); | ||
} | ||
</code></pre> |
17 changes: 17 additions & 0 deletions
17
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInConfigure.cshtml.cs
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,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn | ||
{ | ||
public class LoggedInConfigureModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInUser.cshtml
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,25 @@ | ||
@page | ||
@model Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn.LoggedInUserModel | ||
@{ | ||
ViewData["Title"] = "AuthBuiltIn.LoggedInUser"; | ||
} | ||
|
||
<h3>AuthBuiltIn.LoggedInUser</h3> | ||
<p> | ||
You can test whether a user is logged in using the <code>User</code> instance | ||
</p> | ||
<pre><code> | ||
public class LoggedInUserModel : PageModel | ||
{ | ||
public IActionResult OnGet() | ||
{ | ||
if (User.Identity?.IsAuthenticated != true) | ||
return Challenge(); | ||
|
||
return Page(); | ||
} | ||
} | ||
</code></pre> | ||
|
||
<a class="nav-link text-primary" asp-area="" asp-page="/Index">Back to home page</a> | ||
|
21 changes: 21 additions & 0 deletions
21
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/LoggedInUser.cshtml.cs
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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn | ||
{ | ||
public class LoggedInUserModel : PageModel | ||
{ | ||
public IActionResult OnGet() | ||
{ | ||
if (User.Identity?.IsAuthenticated != true) | ||
return Challenge(); | ||
|
||
return Page(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/Public.cshtml
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,13 @@ | ||
@page | ||
@model Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn.PublicModel | ||
@{ | ||
ViewData["Title"] = "AuthBuiltIn.Public"; | ||
} | ||
|
||
<h3>AuthBuiltIn.Public</h3> | ||
<p> | ||
A normal razor page is, by default, public | ||
</p> | ||
|
||
<a class="nav-link text-primary" asp-area="" asp-page="/Index">Back to home page</a> | ||
|
16 changes: 16 additions & 0 deletions
16
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/Public.cshtml.cs
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,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn | ||
{ | ||
public class PublicModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Example1.RazorApp.IndividualAccounts/Pages/AuthBuiltIn/UserRoles.cshtml
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 @@ | ||
@page | ||
@model Example1.RazorApp.IndividualAccounts.Pages.AuthBuiltIn.PublicModel | ||
@{ | ||
ViewData["Title"] = "AuthBuiltIn.LoggedInAttribute"; | ||
} | ||
|
||
<h3>AuthBuiltIn.LoggedInAttribute</h3> | ||
<p> | ||
By adding <code>[Authorize]</code> to the razor page model means you can only access it if you are logged in e.g., | ||
</p> | ||
<pre><code> | ||
[Authorize] | ||
public class LoggedInAttributeModel : PageModel | ||
{... | ||
</code></pre> | ||
|
||
<a class="nav-link text-primary" asp-area="" asp-page="/Index">Back to home page</a> | ||
|
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
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,23 @@ | ||
# Example1.RazorApp.IndividualAccounts | ||
|
||
This project contains a example of using the AuthPermissions.AspNetCore library in ASP.NET Core razor page web app with user data provided by the individual accounts approach. This is one of the simplest approaches using: | ||
|
||
- **Application**: ASP.NET Core, Razor Pages | ||
- **App type**: Single instance with one database. | ||
- **Database type**: SQL Server (localdb for testing) | ||
- **Users**: ASP.NET Core's individual accounts | ||
- **Roles**: ASP.NET Core's individual accounts | ||
- **AuthenticationType**: Cookie | ||
|
||
The ASP.NET Core code comes comes from the [ASP.NET Core documentation on building razor page web app individual accounts authorization](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/secure-data), but the handling of the visibilty of the contact manager features are handled by the AuthPermissions.AspNetCore library. | ||
|
||
The AuthPermissions.AspNetCore code/features used in this example | ||
|
||
- Mapping the user's Roles to Permissions (read this doc). | ||
- Authorization in razor pages via the `IsAuthorized(<enum permission>)` method. | ||
- UserId data key, plus permissions. | ||
- Add SuperUser on startup feature. | ||
- Admin page to alter the permissions in each role. | ||
|
||
*NOTE: [This article](https://blog.francium.tech/asp-net-core-basic-authentication-authorization-in-razor-pages-with-postgresql-b1f2888b21d0) provides a good overview of the statndard ASP.NET Core authorization approaches.* | ||
|
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
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