Roles available with Windows Authentication

后端 未结 4 1832
旧时难觅i
旧时难觅i 2021-01-31 22:30

I\'m trying to add Roles authentication to an Action in a Controller in an ASP.NET MVC application. The code looks something like this:

[Authorize(Roles = \"Some         


        
相关标签:
4条回答
  • 2021-01-31 22:45

    You can use the various methods on the RoleProvider class in System.Web.Security.Roles.Provider.

    See this for more: Role Provider

    0 讨论(0)
  • 2021-01-31 22:46

    I'm guessing you aren't using a role provider here, but falling back on the underlying functionality of WindowsPrincipal where the roles map to the user's groups. Anyhow, I don't think one can do more than enumerate the windows groups available on that machine/in that domain. Not sure if this helps, but that's all I can say without having an idea of what you are trying to do with said roles list.

    0 讨论(0)
  • 2021-01-31 22:48

    If you don't need to do this programatically, but you are trying to determine the correct Windows Groups/Roles that need to be specified, you can use this from the command line:

    C:\> net group /domain  (lists all Roles in the domain)
    C:\> net user <username> /domain (lists info, including roles for a user)
    

    Otherwise you will need to query the LDAP part of Active Directory, or use something under DirectoryServices.

    Take a look at these websites to access Active Directory via C#:

    • Howto: (Almost) Everything In Active Directory via C# - Codeproject
    • User Management with Active Directory
    0 讨论(0)
  • 2021-01-31 22:49

    Add this to your web.config under system.web:

    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
    

    Then you can use:

    string[] arr = Roles.GetRolesForUser(User.Identity.Name);
    

    or:

    string[] arr = Roles.GetRolesForUser();
    

    0 讨论(0)
提交回复
热议问题