Posts

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 ...

Startup Class in ASP.NET Core

Image
The startup class is the heart of the ASP.NET Core. It helps to configure the application. By default, it has three modes: Startup constructor, ConfigureService Method, and Configure Method. The following is a default MVC Startup class. Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get ; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler...

In-Memory Cache in ASP.NET Core

Image
You can improve the performance of your application by using cache. Instead of generating the content, you can retrieve the content from the cache. You can use cache for rarely changing content. Instead of creating content, the cache will hold the content, so it will be available quickly. ASP.NET Core support IMemoryCache, its cache storage in the memory of the webserver. Let's see how this works. The following is a HomeController code. Here the IMemoryCache interface is implemented using dependency injection. You can see in the controller constructor IMemoryCache object has been passed. In coding, the TryGetValue method checks if there is any cached content for the keyword ‘CacheDateKey’, which will provide content if there is any cached content in it. If there is no content in the cache. It will set the cache, i.e. the time for the cache. Also, the SetSlidingExpiration option is set in the example. This indicates how long the cache should be idle. If any user accesses ...

ASP.NET Core Web API Documentation with Swagger

Image
This blog is going to explain how to implement Swagger documentation in ASP.NET Core. Swagger helps to describe API documents so that consumers can easily understand the API. Swagger UI offers a web-based UI. For the developer, this supports creating quick documentation. All public action methods can be tested using the Swagger UI. Step 1: First, you need to create an API project. New Project -> ASP.NET Core web application -> Provide Project Name-> Click Create button-> Select API -> Click Create Button. After that Create a API controller Empty. Step 2: The next step is to install the Swashbuckle package from the Nuget package. If you are using the NuGet UI, search for Swashbuckle.AspNetCore and install it. Otherwise, use the command below if you are using the Package Manager console. Install-Package Swashbuckle.AspNetCore Step 3: The following is a Movie Model. I have added a summary on top of each property to describe the property Movie.cs pub...

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...

Dependency Injection in ASP.NET Core MVC

Image
This blog is going to explain what Dependency Injection(DI) is and how to implement it in ASP.NET core MVC. Dependency Injection is one of the best practice in ASP.NET Core MVC. The ASP.NET core has a lot of internal components, most of which are created using Dependency Injection. It allows you to create loosely coupled applications. This requires some extra code writing, but it can be useful for applications. Let’s see step by step how to implement Dependency injection in ASPNet Core MVC Step 1: The following is a simple Movie class to store and display the result values. This Movie has three properties MovieName, ReleaseYear and Type public class Movie { public string MovieName { get ; set ; } public int ReleaseYear { get ; set ; } public string Type { get ; set ; } } Step 2: Here I have created an interface called IMovieService and its one methoGetMovieDetails(). So the class that implements the IMovieService interface needs to implement the ...