Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit d5b941b

Browse files
davidfowlDamianEdwards
authored andcommitted
Updated auth features
1 parent 56e2655 commit d5b941b

File tree

1 file changed

+65
-75
lines changed

1 file changed

+65
-75
lines changed

docs/4. Add auth features.md

Lines changed: 65 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,6 @@ In this module we're going to add the capability for users to register and sign-
289289
}
290290
}
291291
```
292-
1. Register the custom `UserClaimsPrincipalFactory<User>` in the `IdentityHostingStartup` class. You can also take this opportunity to tweak the default password policy to be less or more strict if you wish:
293-
``` c#
294-
services.AddDefaultIdentity<User>(options =>
295-
{
296-
options.Password.RequireDigit = false;
297-
options.Password.RequiredLength = 1;
298-
options.Password.RequiredUniqueChars = 0;
299-
options.Password.RequireLowercase = false;
300-
options.Password.RequireUppercase = false;
301-
options.Password.RequireNonAlphanumeric = false;
302-
})
303-
.AddDefaultUI(UIFramework.Bootstrap4)
304-
.AddEntityFrameworkStores<IdentityDbContext>()
305-
.AddClaimsPrincipalFactory<ClaimsPrincipalFactory>();
306-
```
307292
1. Add a new class file `AuthHelpers.cs` in the `Infrastructure` folder and add the following helper methods for reading and setting the admin claim:
308293
``` c#
309294
namespace FrontEnd.Infrastructure
@@ -340,6 +325,14 @@ In this module we're going to add the capability for users to register and sign-
340325
}
341326
}
342327
```
328+
329+
1. Register the custom `UserClaimsPrincipalFactory<User>` in the `IdentityHostingStartup` class:
330+
``` c#
331+
services.AddDefaultIdentity<User>()
332+
.AddDefaultUI(UIFramework.Bootstrap4)
333+
.AddEntityFrameworkStores<IdentityDbContext>()
334+
.AddClaimsPrincipalFactory<ClaimsPrincipalFactory>();
335+
```
343336
1. Add authorization services with an admin policy to the `ConfigureServices()` method of `Startup.cs` that uses the just-added helper methods to require the admin claim:
344337

345338
```csharp
@@ -352,7 +345,7 @@ In this module we're going to add the capability for users to register and sign-
352345
});
353346
});
354347
```
355-
1. Add `Microsoft.AspNetCore.Authorization` to the list of usings in `Index.cshtml.cs`, then use the helper method in the page model to determine if the current user is an administrator.
348+
1. Add `System.Security.Claims` to the list of usings in `Index.cshtml.cs`, then use the helper method in the page model to determine if the current user is an administrator.
356349

357350
```csharp
358351
public bool IsAdmin { get; set; }
@@ -378,21 +371,20 @@ In this module we're going to add the capability for users to register and sign-
378371
@if (Model.IsAdmin)
379372
{
380373
<li>
381-
<a asp-page="/Admin/EditSession" asp-route-id="@session.ID" class="btn btn-default btn-xs">Edit</a>
374+
<a asp-page="/Admin/EditSession" asp-route-id="@session.Id" class="btn btn-default btn-xs">Edit</a>
382375
</li>
383376
}
384377
</ul>
385378
</div>
386379
```
387380
1. Add a nested `Admin` folder to the `Pages` folder then add an `EditSession.cshtml` razor page and `EditSession.cshtml.cs` page model to it.
388-
1. Next, we'll protect pages in the `Admin` folder with an Admin policy by making the following change to the `services.AddMvc()` call in `Startup.ConfigureServices`:
381+
1. Next, we'll protect pages in the `Admin` folder with an Admin policy by making the following change to the `services.AddRazorPages()` call in `Startup.ConfigureServices`:
389382

390383
```csharp
391-
services.AddMvc()
392-
.AddRazorPagesOptions(options =>
393-
{
394-
options.Conventions.AuthorizeFolder("/Admin", "Admin");
395-
})
384+
services.AddRazorPages(options =>
385+
{
386+
options.Conventions.AuthorizeFolder("/Admin", "Admin");
387+
});
396388
```
397389

398390
## Add a form for editing a session
@@ -415,8 +407,7 @@ In this module we're going to add the capability for users to register and sign-
415407
var session = await _apiClient.GetSessionAsync(id);
416408
Session = new Session
417409
{
418-
ID = session.ID,
419-
ConferenceID = session.ConferenceID,
410+
Id = session.Id,
420411
TrackId = session.TrackId,
421412
Title = session.Title,
422413
Abstract = session.Abstract,
@@ -430,59 +421,58 @@ In this module we're going to add the capability for users to register and sign-
430421
1. Add the "{id}" route to the `EditSession.cshtml` form:
431422

432423
```html
433-
@page "{id:int}"
424+
@page "{id}"
434425
@model EditSessionModel
435426
```
436427

437428
1. Add the following edit form to `EditSession.cshtml`:
438429

439430
```html
440-
<h3>Edit Session</h3>
441-
442-
<form method="post" class="form-horizontal">
443-
<div asp-validation-summary="All" class="text-danger"></div>
444-
<input asp-for="Session.ID" type="hidden" />
445-
<input asp-for="Session.ConferenceID" type="hidden" />
446-
<input asp-for="Session.TrackId" type="hidden" />
447-
<div class="form-group">
448-
<label asp-for="Session.Title" class="col-md-2 control-label"></label>
449-
<div class="col-md-10">
450-
<input asp-for="Session.Title" class="form-control" />
451-
<span asp-validation-for="Session.Title" class="text-danger"></span>
452-
</div>
453-
</div>
454-
<div class="form-group">
455-
<label asp-for="Session.Abstract" class="col-md-2 control-label"></label>
456-
<div class="col-md-10">
457-
<textarea asp-for="Session.Abstract" class="form-control"></textarea>
458-
<span asp-validation-for="Session.Abstract" class="text-danger"></span>
459-
</div>
460-
</div>
461-
<div class="form-group">
462-
<label asp-for="Session.StartTime" class="col-md-2 control-label"></label>
463-
<div class="col-md-10">
464-
<input asp-for="Session.StartTime" class="form-control" />
465-
<span asp-validation-for="Session.StartTime" class="text-danger"></span>
466-
</div>
467-
</div>
468-
<div class="form-group">
469-
<label asp-for="Session.EndTime" class="col-md-2 control-label"></label>
470-
<div class="col-md-10">
471-
<input asp-for="Session.EndTime" class="form-control" />
472-
<span asp-validation-for="Session.EndTime" class="text-danger"></span>
473-
</div>
474-
</div>
475-
<div class="form-group">
476-
<div class="col-md-offset-2 col-md-10">
477-
<button type="submit" class="btn btn-primary">Save</button>
478-
<button type="submit" asp-page-handler="Delete" class="btn btn-danger">Delete</button>
479-
</div>
480-
</div>
481-
</form>
482-
483-
@section Scripts {
431+
<h3>Edit Session</h3>
432+
433+
<form method="post" class="form-horizontal">
434+
<div asp-validation-summary="All" class="text-danger"></div>
435+
<input asp-for="Session.Id" type="hidden" />
436+
<input asp-for="Session.TrackId" type="hidden" />
437+
<div class="form-group">
438+
<label asp-for="Session.Title" class="col-md-2 control-label"></label>
439+
<div class="col-md-10">
440+
<input asp-for="Session.Title" class="form-control" />
441+
<span asp-validation-for="Session.Title" class="text-danger"></span>
442+
</div>
443+
</div>
444+
<div class="form-group">
445+
<label asp-for="Session.Abstract" class="col-md-2 control-label"></label>
446+
<div class="col-md-10">
447+
<textarea asp-for="Session.Abstract" class="form-control"></textarea>
448+
<span asp-validation-for="Session.Abstract" class="text-danger"></span>
449+
</div>
450+
</div>
451+
<div class="form-group">
452+
<label asp-for="Session.StartTime" class="col-md-2 control-label"></label>
453+
<div class="col-md-10">
454+
<input asp-for="Session.StartTime" class="form-control" />
455+
<span asp-validation-for="Session.StartTime" class="text-danger"></span>
456+
</div>
457+
</div>
458+
<div class="form-group">
459+
<label asp-for="Session.EndTime" class="col-md-2 control-label"></label>
460+
<div class="col-md-10">
461+
<input asp-for="Session.EndTime" class="form-control" />
462+
<span asp-validation-for="Session.EndTime" class="text-danger"></span>
463+
</div>
464+
</div>
465+
<div class="form-group">
466+
<div class="col-md-offset-2 col-md-10">
467+
<button type="submit" class="btn btn-primary">Save</button>
468+
<button type="submit" asp-page-handler="Delete" class="btn btn-danger">Delete</button>
469+
</div>
470+
</div>
471+
</form>
472+
473+
@section Scripts {
484474
<partial name="_ValidationScriptsPartial" />
485-
}
475+
}
486476
```
487477
1. Add code to handle the `Save` and `Delete` button actions in `EditSession.cshtml.cs`:
488478

@@ -624,7 +614,7 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
624614
public bool RequiresAuthentication { get; set; }
625615

626616
[HtmlAttributeName("authz-policy")]
627-
public string RequiredPolicy { get; set; }
617+
public string RequiredPolicy { get; set; }
628618
```
629619
1. Add a `ViewContext` property:
630620
```csharp
@@ -672,7 +662,7 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
672662
{
673663
output.SuppressOutput();
674664
}
675-
}
665+
}
676666
```
677667
1. Register the new Tag Helper in the `_ViewImports.cshtml` file:
678668
```html
@@ -687,11 +677,11 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
687677
@foreach (var speaker in session.Speakers)
688678
{
689679
<li class="list-inline-item">
690-
<a asp-page="Speaker" asp-route-id="@speaker.ID">@speaker.Name</a>
680+
<a asp-page="Speaker" asp-route-id="@speaker.Id">@speaker.Name</a>
691681
</li>
692682
}
693683
<li authz-policy="Admin">
694-
<a asp-page="/Admin/EditSession" asp-route-id="@session.ID" class="btn btn-default btn-xs">Edit</a>
684+
<a asp-page="/Admin/EditSession" asp-route-id="@session.Id" class="btn btn-default btn-xs">Edit</a>
695685
</li>
696686
</ul>
697687
</div>

0 commit comments

Comments
 (0)