简体   繁体   English

在 Asp.Net MVC 中存储 Authorize 属性的用户列表的最佳方法是什么?

[英]What is the best way to store Users list for a Authorize attribute in Asp.Net MVC?

I want to specify authorize attibute with list of users.我想用用户列表指定授权属性。

This works:这有效:

[Authorize(Users ="g@gmail.com, m@gmail.com")]

But I want to store list of users in one place但我想将用户列表存储在一个地方

I've tried to store them in appSettings:我试图将它们存储在 appSettings 中:

    <appSettings>
    <add key= "admins" value="g@gmail.com, m@gmail.com" />
    </appSettings>

And then make a custom attribute:然后创建一个自定义属性:

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class MyAuthorizeAttribute : AuthorizeAttribute
        {
            public MyAuthorizeAttribute()
            {
                this.Users = WebConfigurationManager.AppSettings["admins"];
            }
        }

And use it before controller:并在控制器之前使用它:

[MyAuthorize]

But for some reasons that doesn't work.但由于某些原因,这不起作用。

If you have any idea on how to realise my purpose or fix my code, please, write.如果您对如何实现我的目的或修复我的代码有任何想法,请写信。

I normally have 2 ways of dealing with this.我通常有两种方法来处理这个问题。

For you it looks like I would stick with AD groups.对您来说,我似乎会坚持使用 AD 组。

So have a file like this.所以有一个这样的文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyProject.Models
{

    public static class AccessLevelRoles
    {
        public const string Admin = "DOMAIN\\MYGROUP";
    }
}

Then you can protect your actions/controllers by using.然后,您可以通过使用来保护您的操作/控制器。

 [Authorize(Roles = AccessLevelRoles.Admin)]

That's probably the easiest way and what you are looking for.这可能是最简单的方法,也是您正在寻找的方法。

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

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