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 c开发者_StackOverflow中文版an use authorize attribute in my application.
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 AuthorizeAttribute
constructor 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
}
}
精彩评论