ASP .Net MVC 4 Authorize and AllowAnonymous

João Martins

I am completely new with this framework and I am still learning the basics of it and C#. Meanwhile, I came across with the attributes Authorize and AllowAnonymous while reading a book and I can't understand how a controller "knows" if the user trying to access those methods/actions is authenticated or not. Where is that information stored? Do I need to to have a special treatment while performing the login method?

Thanks for any help.

Halleck

Assuming that you have some sort of authentication setup in your application (forms authentication, windows authentication or OAuth) a logged in user has a token stored on their browser in the form of a cookie. When a user navigates your application, their token is passed along with them. When the Authorize attribute is applied to one of your controller methods, your application examines their token and if they are an authenticated user with the correct permissions, it allows them in, if not it will redirect them to an action you have specified. The default redirect is to the registration/login page. AllowAnonymous lets users who have not been authenticated access the action or controller.

In short, it knows based on the token it receives from the client.

As for the second question, if you selected "internet application" when you made your MVC 4 project, forms authentication is built in for you and you don't need to do anything but use the generated login action. If you wish to use a database other than the one the generated code makes, you will need to implement MembershipProvider and MembershipUser.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC AllowAnonymous Not working

Regarding Authorize attribute usage in ASP.Net MVC 4

Authorize with a specific scheme in ASP.NET MVC 4

Authorize in ASP mvc4

Custom Authorize Attribute on asp.net mvc

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

ASP.NET MVC 4 Custom Authorize filter on Controller class and Method

Asp.net Core MVC Authorize Attribute not blocking

Asp.Net MVC 5 - Custom Authorize not working?

Override global authorize filter in ASP.NET Core 1.0 MVC

ASP.NET MVC Authorize user with many roles

Authorize only email from specific domain asp.net MVC

Asp.Net MVC authorize a custom user which extends ApplicationUser

Custom middleware (or authorize) for specific route in ASP .NET Core 3.1 MVC

AllowAnonymous not working ASP.Net Core 3.0

asp.net authorize by default (without [Authorize])

How to integrate Authorize.Net payment in ASP.NET MVC and receive payment status?

ASP.NET authorize by page

How to authorize user who logged in using external login in Asp.Net MVC

`[Authorize(Roles = "admin")]` Infinite loop ASP.NET MVC and Azure Active Directory B2C

Adding [Authorize] to controller failing to redirect to Identity Login route. ASP.NET CORE 3.1 MVC

How to create a custom attribute that will redirect to Login if it returns false, similar to the Authorize attribute - ASP.NET MVC

ASP.net MVC - Authorize controller for one user/role but all users for one action

how to authorize users in all pages and respond according to the type of request(ajax or not) in asp.net mvc?

How [Authorize] attribute get to know that the user is authenticate in ASP.NET MVC, is it by using authentication token?

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

ASP.Net MVC 5 how to use Authorize Attribute with multiple login (Multiple user table)

Treeview in asp.net mvc 4

ASP.NET MVC 4 User Authentication

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive