Authorization in ASP NET Core

Поделиться
HTML-код
  • Опубликовано: 11 июн 2019
  • In this video we will discuss, authorization in ASP.NET Core.
    Text version of the video
    csharp-video-tutorials.blogsp...
    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.
    / @aarvikitchen5572
    Slides
    csharp-video-tutorials.blogsp...
    ASP.NET Core Text Articles & Slides
    csharp-video-tutorials.blogsp...
    ASP.NET Core Tutorial
    • ASP.NET core tutorial ...
    Angular, JavaScript, jQuery, Dot Net & SQL Playlists
    ruclips.net/user/kudvenka...
    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.
  • НаукаНаука

Комментарии • 88

  • @lukedodson3441
    @lukedodson3441 5 лет назад +1

    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.

  • @mpauser8399
    @mpauser8399 5 лет назад +7

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

  • @techrelated2417
    @techrelated2417 4 года назад +6

    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.

  • @harithsufri7874
    @harithsufri7874 4 года назад

    Thank you for the detailed explanation!

  • @karthikchinni245
    @karthikchinni245 5 лет назад

    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

  • @waelalghazouli8024
    @waelalghazouli8024 3 года назад

    Thank you very much for your amazing explination!

  • @christianloperadecastro4875
    @christianloperadecastro4875 5 лет назад +2

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

  • @00kiss00L
    @00kiss00L 4 года назад +5

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

  • @user-dv5ps3qd4g
    @user-dv5ps3qd4g 5 лет назад

    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!

    • @naodagere8210
      @naodagere8210 4 года назад

      It works even if you remove that.

    • @rulonoboyev2939
      @rulonoboyev2939 4 года назад

      ​@@naodagere8210 it had to be removed in this lesson as soon as AuthorizeFilter added inConfigureServices

  • @haydarm.al-samawe9819
    @haydarm.al-samawe9819 5 лет назад

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

  • @mohannepal6503
    @mohannepal6503 5 лет назад +1

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

  • @shahidwani6445
    @shahidwani6445 5 лет назад

    Great

  • @jjque322
    @jjque322 Год назад

    great brother!!!!

  • @justonegoodtrade
    @justonegoodtrade 4 года назад

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

  • @lx405
    @lx405 5 лет назад +16

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

    • @amermagdy5440
      @amermagdy5440 4 года назад

      if you got good one please help me

  • @fcs_96
    @fcs_96 5 лет назад

    Thanks for your information. Most useful!

  • @iamanalyst4u
    @iamanalyst4u 5 лет назад +2

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

  • @luismandujano6303
    @luismandujano6303 4 года назад +6

    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?

    • @amonra655
      @amonra655 4 года назад

      1+

    • @mohdnorazmil
      @mohdnorazmil 4 года назад +3

      services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");

    • @puruk.c.152
      @puruk.c.152 3 года назад +3

      When you use the extension method AddIdentity() in your ConfigureServices method of startup class, by default the login path is hard coded to access account/login implicitly(see line number 53 github.com/dotnet/aspnetcore/blob/1f76cce14ac4e4698a554b65a24f28000b50396e/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs). However if you wish to redirect to other page(or you have your login page other than Account/Login) , you can configure it explicitly within your startup.

  • @davenivera8283
    @davenivera8283 3 года назад +3

    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?

    • @elixir662
      @elixir662 2 года назад

      That's the part that's missing it seems. I came here looking for the same thing.

  • @andikita1976
    @andikita1976 2 года назад

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

  • @nolimitsREAL
    @nolimitsREAL 2 года назад

    Does Authorize checks for the token of the user ? I think this is the only way, to check the user. I'm guessing is maintaining a sessions between a front-end and back-end.

  • @MmMm-tg5mq
    @MmMm-tg5mq 5 лет назад

    a little bit confusing I hope that will be clear after watching the rest of videos thanks a lot

  • @NimbuYT
    @NimbuYT 5 лет назад

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

  • @geekrecce9164
    @geekrecce9164 2 года назад

    Hi sir big fan
    xD

  • @kiaanmaharaj
    @kiaanmaharaj 2 года назад

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

  • @kurakulaprasad7898
    @kurakulaprasad7898 2 года назад +1

    Hi Venkat, How it is redirecting to Login Screen when you click on Create or Edit buttons after decorating the actions with Authorize attribute. Can you please let me know where this code is written. As in the start up page it is mentioned the default route Home --> Index. For me after clicking on Edit and Create buttons it is redirecting to Error view.

    • @zgull429
      @zgull429 2 месяца назад

      dear i have same issue ,did you find it ?, kindly help me.

  • @ssushovan
    @ssushovan 3 года назад +1

    How does Asp.Net Core know which Controller/Action to route the incoming request to when Authorize attribute is applied? How is it automatically able to determine the Account/Login action method for logging in?

  • @vivekanandamaity5568
    @vivekanandamaity5568 5 лет назад

    Sir please tell us about jQuery from validation.

  • @pazhanikumar8584
    @pazhanikumar8584 4 года назад

    Dear Venkat,
    How authorize attribute redirects user to login page without any setting or code ? pls any one comment it.

  • @prafulsinghvit
    @prafulsinghvit 3 года назад

    Somebody who understood on how we get redirected to login route, without any configuration, pls let me know, as I am confused on how the application knows that when it sees an [Authorize] filter then it has to redirect to what method ? (there must be some configuration or convention by which it is kicking in the login action method).
    thanks!!

  • @danielfilipe54
    @danielfilipe54 5 лет назад

    How can you generate accountcontroller?

  • @aallal73
    @aallal73 2 года назад

    Hello ,
    When a method is not decorated with [Authorize], how does our application know to redirect to the login page?
    Thanks

  • @HH-yq3sy
    @HH-yq3sy 4 года назад +1

    i missed video where you made redirects to login ... can anyone help me here how to add it. Thanks

    • @HH-yq3sy
      @HH-yq3sy 4 года назад

      I figure it out. When you set service.AddIdentity its has been automaticly set to open method which has [AllowAnonymous]. If login is only method with it then it will open login page

  • @soowenjie3293
    @soowenjie3293 2 года назад

    I follow all the step according to your video but when I login as admin and go to the edit it redirect me to login again. Is there any solution?

  • @wymowaiyan6505
    @wymowaiyan6505 3 года назад

    it doesn't redirect me to login page as you demonstrated. anyone encounter this?

  • @sachingreat222
    @sachingreat222 4 года назад

    How it is redirect to login page on click of edit button

  • @TheAhmedasdasd
    @TheAhmedasdasd 3 года назад

    How to configure Authorization globally in Asp.net Core 3 ?

  • @milandjukic88
    @milandjukic88 4 года назад

    How authorize attribute knows where is login page? Is this default Account/Login?

    • @amonra655
      @amonra655 4 года назад

      if you get answer please tel me

  • @acegaming5317
    @acegaming5317 5 лет назад +1

    Can u make a video on mvc login page?? plzz reply sir

  • @GauravKumar-sr6bt
    @GauravKumar-sr6bt 4 года назад +1

    Where did we configure the URL to login page to which the application redirects if not authorized? If it is default value for login URL, where can we configure it?

  • @conaxlearn8566
    @conaxlearn8566 4 года назад

    How does .Net core know where the login page is? Just by looking for an action called "Login" within all the controllers?

    • @jayasantosh9787
      @jayasantosh9787 4 года назад

      same here, please can you explain if your issues is resolved

    • @conaxlearn8566
      @conaxlearn8566 4 года назад +1

      @@jayasantosh9787 I think source code line 53 on this page is the answer:
      github.com/dotnet/aspnetcore/blob/1f76cce14ac4e4698a554b65a24f28000b50396e/src/Identity/Core/src/IdentityServiceCollectionExtensions.cs
      But I have not worked out how to set a different login page.

  • @dkisov1
    @dkisov1 4 года назад

    ReturnUrl is not binding. Help ?

  • @o_nasrsaad2067
    @o_nasrsaad2067 3 года назад

    How The [Authorize] know that if user not login which redirect login page?
    For more Clear for what I need
    How it know AccountController And Login Action?

  • @ingvarderesivski8907
    @ingvarderesivski8907 4 года назад

    I added Authorize attribute and made login and unable to access to Authorize views. I am using Core 3.2

    • @ramakrishnamb3045
      @ramakrishnamb3045 4 года назад

      In startup.cs add this line : app.UseAuthorization(); after app.UseAuthentication

    • @javaguitarist
      @javaguitarist 4 года назад +2

      For .net core v3.1+, app.UseAuthorization() must be between app.UseRouting() and app.UseEndpoints() in Startup.cs.

  • @hanspetervollhorst1
    @hanspetervollhorst1 3 года назад

    I don't really understand how ASP.NET Core knows that /Account/Login is where the user can enter his login details

  • @samarakhan1574
    @samarakhan1574 4 года назад

    my application is not redirecting me to a login page but it gives 401(unauthorized) error.
    please help me fix this.

    • @enanobap
      @enanobap 4 года назад

      Hi asamara
      add this code in yout start up
      services.ConfigureApplicationCookie(options =>
      {
      options.Events = new CookieAuthenticationEvents
      {
      OnRedirectToLogin = x =>
      {
      x.Response.Redirect("Account/Login");
      return Task.CompletedTask;
      }
      };
      });

    • @AbPoonamhi
      @AbPoonamhi 4 года назад

      @@enanobap After adding above code, still unable to redirect to Login Page. Help Needed.. Thanks in Advance.

    • @connerm8443
      @connerm8443 4 года назад

      @@AbPoonamhi Hi Poonam,
      Is the controller handing your Account/Users called "AccountController"? The default route is "/Account/Login" I believe and if you have named your controller differently this might cause an issue? You can set a custom redirect path using the following method in the ConfigureServices method in Startup.cs:
      services.ConfigureApplicationCookie(options =>
      {
      options.LoginPath = "/Controller/Action";
      });
      Hope this is of help!

    • @radhagobindamishra4649
      @radhagobindamishra4649 4 года назад +1

      @@AbPoonamhi Apply AllowAnonymous attribute to Both Login Action methods.

    • @Fkn1405
      @Fkn1405 3 года назад

      Have you added these middleware in your configure method??
      app.UseAuthentication();
      app.UseAuthorization();
      app.UseEndpoints(endpoints =>.........

  • @swathichittani8341
    @swathichittani8341 2 года назад

    is source code available ?

  • @jjque322
    @jjque322 Год назад

    Do you have an Udemy Course, if not you should have one, can become rich!!!!