Skip to content

Commit

Permalink
Added User Claims display
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPSmith committed Jul 3, 2021
1 parent 3130c8e commit 1052139
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Example1.RazorPages.IndividualAccounts/Model/AppSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AppSummary
public string Application { get; } = "ASP.NET Core, Razor Pages";
public string AuthorizationProvider { get; } = "ASP.NET Core's individual accounts";
public string CookieOrToken { get; } = "Cookie";
public string DataKey { get; } = "- not used -";
public string MultiTenant { get; } = "- not used -";
public string[] Databases { get; } = new []
{
"Individual accounts: InMemoryDatabase",
Expand Down
2 changes: 1 addition & 1 deletion Example1.RazorPages.IndividualAccounts/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<li><strong>@nameof(AppSummary.Application)</strong>:&nbsp;@Model.AppSummary.Application</li>
<li><strong>@nameof(AppSummary.AuthorizationProvider)</strong>:&nbsp;@Model.AppSummary.AuthorizationProvider</li>
<li><strong>@nameof(AppSummary.CookieOrToken)</strong>:&nbsp;@Model.AppSummary.CookieOrToken</li>
<li><strong>@nameof(AppSummary.DataKey)</strong>:&nbsp;@Model.AppSummary.DataKey</li>
<li><strong>@nameof(AppSummary.MultiTenant)</strong>:&nbsp;@Model.AppSummary.MultiTenant</li>
<li>
<strong>@nameof(AppSummary.Databases)</strong>:&nbsp;
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
<a class="nav-link text-dark" asp-area="" asp-page="/UserClaims">User's claims</a>
</li>
</ul>
<partial name="_LoginPartial" />
Expand Down
20 changes: 20 additions & 0 deletions Example1.RazorPages.IndividualAccounts/Pages/UserClaims.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@page
@model UserClaimsModel

<p>Current logged in user claims.</p>

@if (Model.ThisUser?.Identity.IsAuthenticated == true)
{
<h4>User '@User.Identity.Name'</h4>
<ul>

@foreach (var claim in @Model.ThisUser.Claims)
{
<li>@claim.ToString()</li>
}
</ul>
}
else
{
<h4>No user is logged in.</h4>
}
16 changes: 16 additions & 0 deletions Example1.RazorPages.IndividualAccounts/Pages/UserClaims.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Example1.RazorPages.IndividualAccounts.Pages
{
public class UserClaimsModel : PageModel
{
public ClaimsPrincipal ThisUser { get; set; }

public void OnGet()
{
ThisUser = User;
}
}
}
7 changes: 4 additions & 3 deletions Example1.RazorPages.IndividualAccounts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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:

This article (!!! LINK !!!) details how this example was built, and how it works.

- **Application**: ASP.NET Core, Razor Pages
- **AuthorizationProvider**: ASP.NET Core's individual accounts
- **CookieOrToken**: Cookie
- **DataKey**: not used
- **MultiTenant**: not used
- **Databases**: Two databases
- Individual accounts: InMemoryDatabase:
- AuthPermissions: In-memory database (uses SQLite in-memory).
- **Focused on**: The basics of the AuthPermissions authorization system.

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.

Expand All @@ -21,8 +24,6 @@ The AuthPermissions.AspNetCore code/features used in this example
- Authorization in razor pages via the `User.HasPermission(<enum permission>)` method.
- Add SuperUser on startup feature.

This article (!!! LINK !!!) details how this example was built, and how it works.

NOTE: This example does not include the admin pages for

*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 standard ASP.NET Core authorization approaches.*
Expand Down

0 comments on commit 1052139

Please sign in to comment.