简体   繁体   English

从web.config授权Roles MVC Web

[英]Authorize Roles MVC web from web.config

I got the below compilation error when I added "System.Configuration.ConfigurationManager.AppSettings["ADGroupReader"].ToString()" to the authorize role section header. 将“ System.Configuration.ConfigurationManager.AppSettings [“ ADGroupReader”]。ToString()“添加到授权角色部分标题时,出现以下编译错误。

In the web.config I have: add key="ADGroupReader" value="Readers DEV" 在web.config中,我具有:添加key =“ ADGroupReader” value =“阅读器DEV”

Compilation error: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type 编译错误:属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

[AuthorizedRedirect]
[Authorize(Roles = System.Configuration.ConfigurationManager.AppSettings["ADGroupReader"].ToString())]    
public class HomeController : Controller
{
    .....
}

I do not want to hard code the role (Roles="Readers DEV"); 我不想对角色进行硬编码(Roles =“ Readers DEV”); I would like to read it from the web.config. 我想从web.config中读取它。 How can I do that? 我怎样才能做到这一点?

This attributes tutorial explains attribute parameter restrictions: 属性教程介绍了属性参数限制:

Attribute parameters are restricted to constant values of the following types: 属性参数限制为以下类型的常量值:

  • Simple types (bool, byte, char, short, int, long, float, and double) 简单类型(bool,byte,char,short,int,long,float和double)
  • string
  • System.Type 系统类型
  • enums 枚举
  • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.) 对象(类型为对象的属性参数的参数必须为上述类型之一的常数。)
  • One-dimensional arrays of any of the above types 以上任何类型的一维数组

From description above, this assignment is invalid due to existence of ToString method: 根据上面的描述,由于存在ToString方法,因此该分配无效:

[Authorize(Roles = System.Configuration.ConfigurationManager.AppSettings["ADGroupReader"].ToString())]

As a workaround, you can create a custom AuthorizeAttribute with predefined Roles parameter which contains default assignment to Roles with your AppSettings : 解决方法是,您可以使用预定义的Roles参数创建自定义AuthorizeAttribute ,其中包含通过AppSettingsRoles默认分配:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public CustomAuthorizeAttribute() 
    {
        this.Roles = ConfigurationManager.AppSettings["ADGroupReader"].ToString();
    }

    // other stuff
}

Usage in controller class: 控制器类中的用法:

[AuthorizedRedirect]
[CustomAuthorize]
public class HomeController : Controller
{
    .....
}

I solved it this way 我这样解决了

Created a derived class ReaderAuthorizeAttribute 创建一个派生类ReaderAuthorizeAttribute

public class ReaderAuthorizeAttribute : AuthorizeAttribute
{
    public ReaderAuthorizeAttribute()
    {            
        this.Roles = System.Configuration.ConfigurationManager.AppSettings["ADGroupReader"];
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return base.AuthorizeCore(httpContext);
    }
}

Then added [LoteReaderAuthorizeAttribute] 然后添加了[LoteReaderAuthorizeAttribute]

[AuthorizedRedirect]
[ReaderAuthorizeAttribute]    
public class HomeController : Controller
{
   ....
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM