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
Related Q & A:
- What can Node.js do that ASP.NET cannot?Best solution by Stack Overflow
- How to use angular.js with strongly typed view in asp.net-MVC?Best solution by ojdevelops.com
- How I can create timer in classic asp?Best solution by stackoverflow.com
- Where and how I can use mysql_close?Best solution by Stack Overflow
- How do I download a picture so I can use it on my profile?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.