简体   繁体   English

在MVC上设置用户角色

[英]Set user roles on MVC

I need some help, I am making a role based menu. 我需要帮助,正在制作基于角色的菜单。 I'm using LDAP Active Directory to log In. 我正在使用LDAP Active Directory登录。

I can log in but I cannot get the roles from the groups of AD. 我可以登录,但无法从广告组中获取角色。

I try to use a role provider but cant get it to work. 我尝试使用角色提供程序,但无法使其正常工作。 I get the groups using: 我使用以下方法获取组:

private ArrayList setRoles()
{
    ArrayList rolesList = new ArrayList();
    DirectoryEntry de = new DirectoryEntry("LDAP://**********");
    DirectorySearcher ds = new DirectorySearcher(de);
    ds.PropertiesToLoad.Add("memberOf");
    ds.SearchScope = SearchScope.Subtree;
    ds.Filter = "(sAMAccountName=test)"; // your username

    SearchResult result = ds.FindOne();

    foreach (string g in result.Properties["memberOf"])
        rolesList.Add(g);
    return rolesList;
}

Now, I need to "set" the roles somewhere in order to use 现在,我需要在某个位置“设置”角色才能使用

User.IsInRole("Admin")

and

[Authorize role...]
public bla bla bla()

Any ideas, links, etc? 有任何想法,链接等吗?

PD: IM USING FORMS AUTH. PD:IM使用格式授权。

You shouldn't have to manually do this. 您不必手动执行此操作。 Using the built in role provider should accomplish the task by setting it in the web.config. 使用内置角色提供程序应该通过在web.config中进行设置来完成任务。

Active Directory Membership Provider Active Directory成员资格提供程序

Windows Token Role Provider Windows令牌角色提供程序

UPDATE: Here's a question on StackOverflow that covers setting this up with an ActiveDirectory membership provider but still using Forms Authentication. 更新:这是一个关于StackOverflow的问题,涉及使用ActiveDirectory成员资格提供程序进行设置,但仍使用表单身份验证。

ASP.NET MVC - Authenticate users against Active Directory, but require username and password to be inputted ASP.NET MVC-根据Active Directory对用户进行身份验证,但需要输入用户名和密码

I do not have experience working with AD, however, what you need to do is set the User property of HttpContext with a Principal that has the roles. 我没有使用AD的经验,但是,您需要用具有角色的Principal设置HttpContext的User属性。

An approach I have previously created was to create a custom authorization attribute that inherits from AuthorizationAttribute. 我以前创建的一种方法是创建一个从AuthorizationAttribute继承的自定义授权属性。

public class AuthorizeActiveDirectoryAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var user = filterContext.HttpContext.User;

        //Your code to get the list of roles for the current user

        var formsIdentity = filterContext.HttpContext.User.Identity as FormsIdentity;
        filterContext.HttpContext.User = new System.Security.Principal.GenericPrincipal(formsIdentity, rolesList.ToArray());

        base.OnAuthorization(filterContext);
    }

}

You would then apply it to your action methods 然后将其应用于您的操作方法

[AuthorizeActiveDirectory role...]
public bla bla bla()

This will also allow you to use User.IsInRole("Admin") 这也将允许您使用User.IsInRole("Admin")

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

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