Unit-8 Securing in ASP
Unit-8 Securing in ASP
Authorization—The process of
determining whether the requested
action is allowed.
Authentication
The CLI Commands for MVC, Razor Pages and Blazor (Server), respectively:
> dotnet new mvc --auth Individual -o MVCDemo
> dotnet new webapp --auth Individual -o PageDemo
> dotnet new blazorserver --auth Individual -o BlazorDemo
The dotnet new command is followed by the template name (mvc, webapp,
blazorserver).
The –auth option allows you to specify the authentication type, e.g. Individual
The -o option is an optional parameter that provides the output folder name for the
Create a Web App with Authentication
Create an ASP.NET Core Web Application project with Individual User Accounts.
The generated project provides ASP.NET Core Identity as a Razor Class Library. The
Identity Razor Class Library exposes endpoints with the Identity area. For example:
/Identity/Account/Login
/Identity/Account/Logout
/Identity/Account/Manage
Install required packages listed below.
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
PM> Update-Database
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
The template-generated app doesn't use authorization. app.UseAuthorization is included to
ensure it's added in the correct order should the app add authorization. UseRouting,
UseAuthentication, UseAuthorization, and UseEndpoints must be called in the order shown in
the preceding code.
Authorization
[Authorize]
public IActionResult AuthedUsersOnly()
{
return View();
}
}
Authorization: Roles, Claims and Policies
When an identity is created it may belong to one or more roles. For
example, Tracy may belong to the Administrator and User roles whilst
Scott may only belong to the User role. How these roles are created and
managed depends on the backing store of the authorization process. Roles
are exposed to the developer through the IsInRole method on the
ClaimsPrincipal class.
Role
Role-based authorization checks are declarative—the developer embeds them within their code,
against a controller or an action within a controller, specifying roles which the current user
must be a member of to access the requested resource.
For example, the following code limits access to any actions on the
AdministrationController to users who are a member of the Administrator role:
[Authorize(Roles = "Administrator")]
public class AdministrationController : Controller
{
}
You can specify multiple roles as a comma separated list:
[Authorize(Roles = "HRManager,Finance")]
public class SalaryController : Controller
{
}
This controller would be only accessible by users who are members of the HRManager role or the
Finance role.
If you apply multiple attributes then an accessing user must be a member of all the roles
specified; the following sample requires that a user must be a member of both the Employee
and Teacher role.
[Authorize(Roles = “Employee")]
[Authorize(Roles = “Teacher")]
public class ReportController : Controller
{
}
You can further limit access by applying additional role authorization attributes at the action level
[Authorize(Roles = "Parent, Teacher")]
public class ProgressReportController : Controller
{
public ActionResult View()
{
}
[Authorize(Roles = "Teacher")]
public ActionResult Download()
{
}
}
In the previous code snippet members of the Parent role or the Teacher role can access
the controller and the view action, but only members of the Teacher role can access
the download action.
You can also lock down a controller but allow anonymous, unauthenticated access to
individual actions.
[Authorize]
public class TimeTableController : Controller
{
[AllowAnonymous]
public ActionResult View()
{
}
[Authorize(Roles = "Teacher")]
public ActionResult Download()
{
}
}