You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 10, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/4. Add auth features.md
+24-68Lines changed: 24 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -107,48 +107,47 @@ In this module we're going to add the capability for users to register and sign-
107
107
```
108
108
109
109
### Allow creation of an admin user
110
-
>Let's make it so the site allows creation of an admin user when there isn'tonealready, butonlyiftheuseralsohasaspecialsingle-usecreationkey. Thatway, wecaneasilycreateanadminuserwithoutaccesstothedatabasewhenwefirstruntheappinanyenvironment.
110
+
>Let's make it so the site allows creation of an admin user when there isn'tonealready. Thefirstusertoaccessthesitewillbedeemedtheadministrator.
111
111
112
112
1. Createanew class `AdminService` in the `Services` folder. This classwill be responsible for managing the creation key generation and tracking whether the site should allow creating admin users.
113
113
1. Add code to the class that will create an appropriately long creation key and expose it via a property:
1. Updatethe `RegisterModel` classin the `Register.cshtml.cs` file to accept `IAdminService` and `IdentityDbContext` parameters and save them to local members:
166
+
1. Updatethe `RegisterModel` classin the `Register.cshtml.cs` file to accept `IAdminService` as a parameter and it them to a local member:
168
167
``` c#
169
168
[AllowAnonymous]
170
169
publicclassRegisterModel : PageModel
@@ -174,52 +173,30 @@ In this module we're going to add the capability for users to register and sign-
174
173
privatereadonlyILogger<RegisterModel> _logger;
175
174
privatereadonlyIEmailSender_emailSender;
176
175
privatereadonlyIAdminService_adminService;
177
-
privatereadonlyIdentityDbContext_dbContext;
178
176
179
177
publicRegisterModel(
180
178
UserManager<User> userManager,
181
179
SignInManager<User> signInManager,
182
180
ILogger<RegisterModel> logger,
183
181
IEmailSenderemailSender,
184
-
IAdminServiceadminService,
185
-
IdentityDbContextdbContext)
182
+
IAdminServiceadminService)
186
183
{
187
184
_userManager=userManager;
188
185
_signInManager=signInManager;
189
186
_logger=logger;
190
187
_emailSender=emailSender;
191
188
_adminService=adminService;
192
-
_dbContext=dbContext;
193
189
}
194
190
195
191
...
196
192
```
197
-
1. Add a `bool` property to the page model to indicate to the page whether admin creation is currently allowed:
198
-
``` c#
199
-
publicboolAllowAdminCreation { get; set; }
200
-
```
201
-
1. Add an `AdminCreationKey` property to to the page's `InputModel` classto capture the submitted key for creating the admin user:
202
-
``` c#
203
-
[DataType(DataType.Password)]
204
-
[Display(Name = "Admin creation key")]
205
-
publiclong? AdminCreationKey { get; set; }
206
-
```
207
-
1. Add code to the `OnGet` method to use the `IAdminService` to see if admin creation is enabled and log the creation key if so. You'll also need to change the method to be async by updating the method signature to the following: `publicasyncTaskOnGetAsync(stringreturnUrl=null)`
193
+
194
+
1. Add code to the `OnPostAsync` that marks the new user as an admin if the `IAdminService.AllowAdminUserCreationAsync` returns true before creating the user:
208
195
``` c#
209
196
if (await_adminService.AllowAdminUserCreationAsync())
210
-
{
211
-
AllowAdminCreation=true;
212
-
_logger.LogInformation("Admin creation is enabled. Use the following key to create an admin user: {adminKey}", _adminService.CreationKey);
213
-
}
214
-
```
215
-
1. Add code to the `OnPostAsync` that marks the new user as an admin if the admin creation key was submitted and matches the in the `IAdminService`, before creating the user:
216
-
``` c#
217
-
if (await_adminService.AllowAdminUserCreationAsync() && Input.AdminCreationKey == _adminService.CreationKey)
218
197
{
219
198
// Set as admin user
220
199
user.IsAdmin=true;
221
-
// In the event user creation fails in the next few lines, set this so the admin key box still shows up on the retry page
>Ifyouruntheappatthispoint, you'll see an exception stating that you can'tinjectascopedtypeintoatyperegisteredasasingleton. ThisistheDIsystemprotectingyoufromacommonanti-patternthatcanarise when using IoC containers. Let's fix the `AdminService` to use the scoped `IdentityDbContext` correctly.
0 commit comments