简体   繁体   English

如何在ASP.NET MVC应用程序中使用Authorize属性?

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

i have a MVC application who have their own authenticate system. 我有一个拥有自己的身份验证系统的MVC应用程序。 he used their own system and check the user by cookie parsing everytime. 他使用了自己的系统,并每次通过cookie解析来检查用户。

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. 谁能告诉我如何在应用程序中使用authorize属性。

You can use the AuthroizeAttribute either on the Controller level: 您可以在控制器级别使用AuthroizeAttribute

[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. 您还可以将用户名,角色等传递给AuthorizeAttribute构造函数,以实现更精细的授权。

If, however, the default AuthroizeAttribute doesn't work for you you can always roll your own by inheriting from AuthorizeAttribute : 但是,如果默认的AuthroizeAttribute对您不起作用,则可以始终通过继承AuthorizeAttribute来滚动自己的AuthroizeAttribute

public CustomAuthorizeAttribute : AuthorizeAttribute 
{
    public override void OnAuthorization(AuthorizationContext filterContext) 
    {            
        base.OnAuthorization(filterContext);

        // Auhtorization logic here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC应用程序的“特殊授权”属性 - Specialized Authorize attribute for asp.net mvc application 有没有人对[Authorize]属性如何处理标准ASP.NET MVC 5应用程序中的路由有一个很好的解释? - Does anyone have a good explanation on how the [Authorize] Attribute handles routes in a standard ASP.NET MVC 5 application? asp.net mvc授权属性重定向 - asp.net mvc authorize attribute redirect 在ASP.NET MVC上的自定义授权属性 - Custom Authorize Attribute on asp.net mvc 带参数的ASP.NET MVC Authorize属性 - ASP.NET MVC Authorize attribute with a parameter ASP.NET MVC和自定义授权属性 - ASP.NET MVC and custom authorize attribute ASP.Net MVC 5如何在多个登录名(多个用户表)中使用授权属性 - ASP.Net MVC 5 how to use Authorize Attribute with multiple login (Multiple user table) 如何返回false,如何创建将重定向到Login的自定义属性,类似于Authorize属性 - ASP.NET 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 WebForms 应用程序迁移到 ASP.NET MVC 应用程序? - How i can migrate a short ASP.NET WebForms Application to ASP.NET MVC Application? 因此,在asp.net MVC中应用授权属性没有捷径吗? - So, there is no shortcut in applying authorize attribute in asp.net MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM