@@ -289,21 +289,6 @@ In this module we're going to add the capability for users to register and sign-
289
289
}
290
290
}
291
291
```
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
- ```
307
292
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:
308
293
``` c #
309
294
namespace FrontEnd .Infrastructure
@@ -340,6 +325,14 @@ In this module we're going to add the capability for users to register and sign-
340
325
}
341
326
}
342
327
```
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
+ ```
343
336
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 :
344
337
345
338
```csharp
@@ -352,7 +345,7 @@ In this module we're going to add the capability for users to register and sign-
352
345
});
353
346
});
354
347
```
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.
356
349
357
350
``` csharp
358
351
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-
378
371
@if (Model.IsAdmin)
379
372
{
380
373
<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 >
382
375
</li >
383
376
}
384
377
</ul >
385
378
</div >
386
379
```
387
380
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 ` :
389
382
390
383
``` 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
+ });
396
388
```
397
389
398
390
## 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-
415
407
var session = await _apiClient .GetSessionAsync (id );
416
408
Session = new Session
417
409
{
418
- ID = session .ID ,
419
- ConferenceID = session .ConferenceID ,
410
+ Id = session .Id ,
420
411
TrackId = session .TrackId ,
421
412
Title = session .Title ,
422
413
Abstract = session .Abstract ,
@@ -430,59 +421,58 @@ In this module we're going to add the capability for users to register and sign-
430
421
1 . Add the "{id}" route to the ` EditSession.cshtml ` form:
431
422
432
423
``` html
433
- @page "{id:int }"
424
+ @page "{id}"
434
425
@model EditSessionModel
435
426
```
436
427
437
428
1. Add the following edit form to `EditSession.cshtml`:
438
429
439
430
```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 {
484
474
<partial name =" _ValidationScriptsPartial" />
485
- }
475
+ }
486
476
```
487
477
1 . Add code to handle the ` Save ` and ` Delete ` button actions in ` EditSession.cshtml.cs ` :
488
478
@@ -624,7 +614,7 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
624
614
public bool RequiresAuthentication { get ; set ; }
625
615
626
616
[HtmlAttributeName (" authz-policy" )]
627
- public string RequiredPolicy { get ; set ; }
617
+ public string RequiredPolicy { get ; set ; }
628
618
```
629
619
1 . Add a ` ViewContext ` property:
630
620
``` csharp
@@ -672,7 +662,7 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
672
662
{
673
663
output .SuppressOutput ();
674
664
}
675
- }
665
+ }
676
666
```
677
667
1 . Register the new Tag Helper in the ` _ViewImports.cshtml ` file:
678
668
``` html
@@ -687,11 +677,11 @@ We're currently using `if` blocks to determine whether to show parts of the UI b
687
677
@foreach (var speaker in session.Speakers)
688
678
{
689
679
<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 >
691
681
</li >
692
682
}
693
683
<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 >
695
685
</li >
696
686
</ul >
697
687
</div >
0 commit comments