Windows身份验证可用的角色

Guy*_*Guy 12 asp.net-mvc windows-authentication

我正在尝试将角色身份验证添加到ASP.NET MVC应用程序中的Controller中的Action.代码看起来像这样:

[Authorize(Roles = "SomeRoleName")]
public ActionResult Index()
{
    bool inRole = User.IsInRole("Admin");
Run Code Online (Sandbox Code Playgroud)

如果我删除Authorize属性并在该代码示例的最后一行放置一个断点,有没有办法可以检查对象并找出可用的角色?

例如,我在立即窗口中调用User.IsInRole("Admin"),它将给我一个真/假值.如何访问可用角色集合?

Bry*_*ein 16

如果您不需要以编程方式执行此操作,但是您尝试确定需要指定的正确Windows组/角色,则可以从命令行使用此命令:

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

否则,您将需要查询Active Directory的LDAP部分,或使用DirectoryServices下的内容.

看一下这些网站,通过C#访问Active Directory:


bea*_*oss 7

将其添加到system.web下的web.config中:

<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>
Run Code Online (Sandbox Code Playgroud)

然后你可以使用:

string[] arr = Roles.GetRolesForUser(User.Identity.Name);
Run Code Online (Sandbox Code Playgroud)

要么:

string[] arr = Roles.GetRolesForUser();
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述