Lab 13
Lab 13
Lab 13
The scaffolding feature will configure Asp.Net Core Identity and add Razor pages for identity
management.
o Select an existing layout page: let it empty because the _Layout is configured in the
_ViewStart.cshtml
o Choose files to override: select
Account\Login
Account\Logout
Account\Register
Account\Manage\Index
Account\Manage\SetPassword
Account\Manage\ChangePassword
Account\Manage\PersonalData
context.Configuration.GetConnectionString("IdentityContextConnection"),
x=>x.MigrationsAssembly("Ansej.Sidjeme.Web")));
services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<IdentityContext>();
1. Note the use of a connection string named IdentityContextConnection.
2. In the Ansej.Sidjeme.Web project, open appsettings.json file.
3. Notice that IdentityContextConnection connection string is present.
4. In the Views/Shared folder, remark that two partial views were added,
_CookieConsentPartial.cshtml and _LoginPartial.cshtml. The first renders a partial view to accept
the use of cookies as you see on websites; the second renders its content according to the
authentication information (if anonymous access, it displays connection and registration links if
not displays the information of the logged in user and the logout link).
5. In the Views/Shared/Components folder, open the Default.cshtml view which represents the
application menu.
6. Locate the /*TODO: Embed the _LoginPartial view*/ comment just after the comment
embed the _LoginPartial view using the <partial> tag helper.
7. Open the Startup.cs file and locate the comment //TODO: MapRazorPages, just after it, Map
Razor Pages using the endpoints object.
Task 2: Requiring Authenticated user on specific resource (Disabling Anonymous access to resource)
1. In the Ansej.Sidjeme.Web project, open Startup.cs.
2. Locate the //TODO: Enable Authorization middleware, just after the comment enable the
Authorization middleware.
3. Under Controllers folder, open SessionValidationController.cs file.
4. Locate the //TODO: Import Microsoft.AspNetCore.Authorization namespace comment, just
after it import the asked namespace.
5. Locate the comment //TODO: Add the Authorize attribute to disable anonymous access
to this controller; just after it and above the class definition Add [Authorize] attribute.
6. Build the solution and ensure there are no errors.
7. Run the solution.
8. In the Sidjeme App, ensure the user is not logged in (If so, click the logout link).
9. In the main menu click Sessions and ensure that your redirected to the login page. In other
words, the access to this resource is not allowed to anonym user.
10. Login using the user your registered previously, and try to access to the Sessions page; this time
you can access the page.
11. Close the browser and stop debugging.
Exercise 3: Configuring Asp.Net Core Authorization
Scenario
In this exercise, you will configure Asp.Net Core Authorization. Explore different options to secure
access to resources according to roles or policies.
o.Filters.Add(new AuthorizeFilter(policy));
}
3. Build the solution and ensure there are no errors.
4. Run the solution.
5. Note that all pages are now inaccessible if you are not authenticated; the only accessible are
those marked with [AllowAnonymous] attribute like Register page, Login Page.
6. Close the browser and stop Debugging.
7. Under Areas/Identity/Pages/Account folder, open the Register.cshtml page then press F7 to
access the page code behind.
8. Comment out the [AllowAnonymous] attribute to disable registering feature for
unauthenticated users.
9. You will, configure the correct access policy later in this exercise.