我觉得这个问题值得与代码示例的答案...以@ tvanfosson的建议扩展AuthorizeAttribute
班,这是我想出的(批评不止是欢迎)。
AuthorizeFromConfiguration.cs:
public class AuthorizeFromConfiguration: AuthorizeAttribute
{
public new string Roles
{
get {
return base.Roles;
}
set {
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("authorization.json")
.Build();
base.Roles = config[value];
}
}
}
authorization.json:
{
"Parts": {
"Create": "contoso.com\\MyWebApp_CreateNewPart",
"Edit": "contoso.com\\MyWebApp_EditPart"
}
}
示例用法:
[AuthorizeFromConfiguration(Roles = "Parts:Create")]
public class CreateModel : PageModel
{
//...
}
注意:在我的测试中,在对authorization.json
文件所做的任何更改生效之前,必须重新启动网站,即使当我尝试更改逻辑以便在get
访问器而不是set
上读取JSON文件时也是如此。
是的,经过更多的谷歌搜索,看起来像是唯一的出路。 – 2009-11-12 20:52:35