How to use Authorize Attribute in asp.net MVC?

how i can use Authorize attribute in ASP.NET MVC application?

  • i have a MVC application who have their own authenticate system. he used their own system and check the user by cookie parsing everytime. how i can put the attribute Authorize on the action wherever i have information about the current user. the user type is a enum who have a part of struct user {} can anyone show me how i can use authorize attribute in my application.

  • Answer:

    You can use the AuthroizeAttribute either on the Controller level: [Authorize] public class HomeController : Controller { // Now all actions require authorization } or Action level: public class HomeController : Controller { public ActionResult Index() { // Does not require authorization } [Authorize] public ActionResult PrivateThing() { // requires authorization } } You can pass usernames, roles, etc to the http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx as well for finer grained authorization. If, however, the default AuthroizeAttribute doesn't work for you you can always roll your own by inheriting from AuthorizeAttribute: public CustomAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); // Auhtorization logic here } }

delete my account at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.