Authorization in ASP NET Core

preview_player
Показать описание
In this video we will discuss, authorization in ASP.NET Core.

Text version of the video

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

What is Authorization in ASP.NET Core

Authentication is the process of identifying who the user is.
Authorization is the process of identifying what the user can and cannot do.
For example, if the logged in user is an administrator he may be able to Create, Read, Update and Delete orders, where as a normal user may only view orders but not Create, Update or Delete orders.
Authorization in ASP.NET Core MVC is controlled through the AuthorizeAttribute

Authorize Attribute in ASP.NET Core
When the Authorize Attribute is used in it's simplest form, without any parameters, it only checks if the user is authenticated.

Authorize Attribute Example

As the Authorize attribute is applied on the Controller, it is applicable to all the action methods in the controller. The user must be logged in, to access any of the controller action methods.

[Authorize]
public class HomeController : Controller
{
public ViewResult Details(int? id)
{
}

public ViewResult Create()
{
}

public ViewResult Edit(int id)
{
}
}

Authorize attribute can be applied on individual action methods as well. In the example below, only the Details action method is protected from anonymous access.

public class HomeController : Controller
{
[Authorize]
public ViewResult Details(int? id)
{
}

public ViewResult Create()
{
}

public ViewResult Edit(int id)
{
}
}

AllowAnonymous Attribute in ASP.NET Core

As the name implies, AllowAnonymous attribute allows anonymous access. We generally use this attribute in combination with the Authorize attribute.

AllowAnonymous Attribute Example

As the Authorize attribute is applied at the controller level, all the action methods in the controller are protected from anonymous access. However, since the Details action methos is decorated with AllowAnonymous attribute, it will be allowed anonymous access.

[Authorize]
public class HomeController : Controller
{
[AllowAnonymous]
public ViewResult Details(int? id)
{
}

public ViewResult Create()
{
}

public ViewResult Edit(int id)
{
}
}

Please note: If you apply [AllowAnonymous] attribute at the controller level, any [Authorize] attribute attributes on the same controller (or on any action within it) is ignored.

Apply Authorize attribute globally

To apply [Authorize] attribute globally on all controlls and controller actions throught your application modify the code in ConfigureServices method of the Startup class.

public void ConfigureServices(IServiceCollection services)
{
// Other Code

services.AddMvc(config =] {
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});

// Other Code
}

AuthorizationPolicyBuilder is in Microsoft.AspNetCore.Authorization namespace
AuthorizeFilter is in Microsoft.AspNetCore.Mvc.Authorization namespace

If you do not have [AllowAnonymous] attribute on the Login actions of the account controller you will get the following error because the application is stuck in an infinite loop.

HTTP Error 404.15 - Not Found

The request filtering module is configured to deny a request where the query string is too long.

Most likely causes:
Request filtering is configured on the Web server to deny the request because the query string is too long.

You try to access /Account/login
Since the [Authorize] attribute is applied globally, you cannot access the URL /Account/login
To login you have to go to /Account/login
So the application is stuck in this infinite loop and every time we are redirected, the query string ?ReturnUrl=/Account/Login is appended to the URL
This is the reason we get the error - Web server denied the request because the query string is too long.

To fix this error, decorate Login() actions in the AccountController with [AllowAnonymous] attribute.
Рекомендации по теме
Комментарии
Автор

Caught up on all the videos in this playlist, by far the best on here. Thank you and will anticipate the rest of the upcoming videos. thanks again.

lukedodson
Автор

I love the way you explain your the best teacher
.
can you make tutorial especially deducted for DOM Manipulation as much as possible

mpauser
Автор

Hi Venkat, I am big fan of your teaching. You're truly gifted. Thanks for all the efforts you put in to make these videos. Would you make series on Identiryserver4 and also, Microservices (with Ocelot gateway) ? Thanks once again.

techrelated
Автор

Thank you for the detailed explanation!

harithsufri
Автор

Don't forget to apply [AllowAnonymous] attribute on the Error Controller!

kissL
Автор

Hi Venkat.. I'm a huge fan of you.. your explanation makes everyone feels to be a professional software engineer with your wonderful playlists
Please do us a favour of staring ReactJS Tutorials too in your style of explanation.. Thanks in advance

karthikchinni
Автор

Thank you very much for your amazing explination!

waelalghazouli
Автор

Thank you for wonderful tutorial. Pls upload part 72. We need more tuts from you.

christianloperadecastro
Автор

Thank you very much for tutorials.
In this video you left the authorize attribute on the controller so we couldn't see if the change in the startup class was worked.
Thank you again!

dvpsqdg
Автор

Hi Venkat! Thank you for all hard work that you have been doing for us. Just request, please do 2-3 videos per day.

mohannepal
Автор

The course is perfect .. im following this .. just i hope you can add video how to use and watching Sass with asp.net core

haydarm.al-samawe
Автор

Thank you so much for this. Is there a way to add specific authorization if the authenticated user has a certain attribute

justonegoodtrade
Автор

Hi venkat, will there also be a playlist about building API's with ASP.NET Core Web Api ? Thanks.

lx
Автор

hi there Venkat Please post a video on (SSO)Single Sign on with ASP.net core

iamanalystu
Автор

@kudvenkat I am using windows authentication in an intranet application. I have followed your tutorial, but I want only certain users or only one group to create edit and delete. How can that be done. Looking for your reply.

andikita
Автор

Thanks for the explanation. I am new to web dev and I have one question in mind. How does the [Authorize] or the ASP know that the user is now authorized? In the video, there is a login method which makes the user authorized. But how? How does the login make someone authorized?

davenivera
Автор

Thanks for your information. Most useful!

fcs_
Автор

Hi Sir, done all CRUD
operation except Delete ? can u please help me with this .

NimbuYT
Автор

Hi, when you use Authorize attribute on Home/Create action method .. How does the program know it has to redirect you to the Login view without been especified?

luismandujano
Автор

Do i implement this in the MVC project or Web Api project if i had to use it?

kiaanmaharaj