Posts

Showing posts with the label Authorization

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC

Image
Are you ready to enhance your ASP.NET Core MVC project with the power of Identity? In this comprehensive guide, we'll walk you through the process of setting up and customizing the Identity system in your web application. By the end of this tutorial, you'll be able to implement secure user authentication and authorization for your ASP.NET Core project. Step 1: Create a New ASP.NET Core Web App Begin by opening Visual Studio and selecting "ASP.NET Core Web App (Model-View-Controller)" as your project template. Follow these steps: 1. Open Visual Studio and create a new project, selecting "ASP.NET Core Web App (Model-View-Controller)". 2. In the "Configure your project" window, enter the project name and choose the project's location. 3. In the "Additional Information" window, select the framework and choose "Individual Accounts" as the Authentication type. 4. Click "Create" to generate your project....

View-Based Authorization in ASP.NET Core

Image
This blog is going to explain what is View-Based Authorization and how to implement it in ASP.NET Core. In some cases, the developer may need to show or hide the UI based on the user's identity. For example, an admin can edit data, but should not allow guest users to edit data. In this case, the developer should show the edit link when the admin user logs in and hide the edit link when the guest user logs in. This process can be achieved through View-Based Authorization . Let's look at that step by step. Step 1: In the first step, I created a Users class to get user details. Also, I added two values to test View-Based Authorization . For real use, you need to get these user details from the database. Users.cs public class Users { public string Username { get ; set ; } public string Password { get ; set ; } public string Role { get ; set ; } public DateTime DateOfBirth { get ; set ; } public IEnumerable<Users> GetUsers() { ...

Policy-Based Authorization in ASP.NET Core

Image
Authorization is the process of determining whether a user is allowed to access a resource. Policy-based authorization helps to verify one or more requirements to authorize the user. The policy-based authorization helps to separate the application logic and authorization logic We will see step by step how to implement policy-based authorization on ASP.NET Core MVC. In this example, the policy-based authorization is going to authorize whether the user is from the state of Florida or not. This will allow the user to access the page only when the user state is Florida Step 1: In the first step, I created a class to get user information. For demo purposes, I have added two data. But in a real application, you need to get information from the database. public class Users { public string Username { get ; set ; } public string Password { get ; set ; } public string State { get ; set ; } public IEnumerable<Users> GetUsers() { return new ...

Role-Based Authorization in ASP.NET Core MVC

Image
This blog is going to explain what Role-Based Authorization is and how it can be implemented in ASP.NET Core MVC. The Authorization is a process of controlling which page the logged-in user can access. Role-Based Authorization authorizes the user based on the user's roles. Let's see how to achieve it step by step. Step 1: The first step is to make sure that the Authorization middleware is included in the Configure method of Startup.cs file . Authentication middleware checks whether the user has permission to place a request for the application. If the user has permission then it will allow creating a response. Authorization middleware should come after Authentication middleware as shown in the code below. Startup.cs public void Configure( IApplicationBuilder app, IWebHostEnvironment env) { ... ... ... app.UseAuthentication(); app.UseAuthorization(); ... ... ... } Step 2: In this step, I have created the Users class. It has thr...

Global Authorization Filter in ASP.NET Core

Image
This blog is going to explain how to authorize the ASP.NET Core application globally. That is, it is not necessary to add the [Authorize] attribute to each controller or action method. The global authorization filter helps you to authorize the entire application. You can achieve this in a few steps. Step 1: Add the following code to the Startup.cs file in ConfigureServices mode. In the code first, the authorization policy has been created using AuthorizationPolicyBuilder() . It has several methods to create authorization policies. Here I have added the RequireAuthenticatedUser() method. It checks whether the user is authenticated or not. Similar to the [Authorize] filter. By adding this method you do not have to add the [Authorize] filter attribute to all controllers and methods. Finally, the global authorization filter is added using config.Filters.Add(new AuthorizeFilter(policy)); Startup.cs public void ConfigureServices( IServiceCollection services) { ... ... ...