Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the [Authorize] attribute for ASP.NET MVC controllers only for Membership Providers?

Does the [Authorize] attribute used with ASP.NET MVC controllers only function with sites that have implemented a MembershipProvider?

like image 461
KingNestor Avatar asked Jun 11 '09 02:06

KingNestor


People also ask

How does Authorize attribute work in ASP.NET MVC?

If a user is not authenticated, or doesn't have the required user name and role, then the Authorize attribute prevents access to the method and redirects the user to the login URL. When both Roles and Users are set, the effect is combined and only users with that name and in that role are authorized.

Where can the Authorize attribute can be applied?

You can place the Authorize attribute on a controller or on individual actions inside the controller. When we place the Authorize attribute on the controller itself, the authorize attribute applies to all of the actions inside.


2 Answers

Short answer is no. It just checks that there is a IPrincipal, how that gets there is up to you.

I have my own login logic that I use instead of the Membership provider, once I've authenticated a user I just call the FormsAuthentication.SetAuthCookie method. Once you've done that you can then use the [Authenticate] attribute.

like image 54
Mark Avatar answered Sep 22 '22 23:09

Mark


The [Authorize] attribute is an action filter. It's going to grab the IPrincipal and check if the user is authenticated or if you specify roles and/or users in with the attribute, it will match against those.

There are many ways that a web request can be authenticated. Everything from Open ID to Windows Authentication. Check out this question for an OpenID example and more links to implementing authentication that way: StackOverflow Question 961468

like image 40
Jeff Avatar answered Sep 21 '22 23:09

Jeff