5

有没有办法我可以使用以下命令在字符串变量中获取角色....

 System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
  System.Security.Principal.WindowsPrincipal wp = new System.Security.Principal.WindowsPrincipal(wi);

我需要这个

 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,                          // version
                                                   UserName.Text,           // user name
                                                   DateTime.Now,               // creation
                                                   DateTime.Now.AddMinutes(60),// Expiration
                                                   false,                      // Persistent 
                                                   role);         // User data

作为字符串角色= wp.IsInRole();
但这不对

类似的东西......

4

6 回答 6

6

您可以从 WindowsIdentity.Groups 属性中获取用户所属的组/角色的列表。WindowsIdentity.Groups 集合仅包含用户所在组/角色的 SID(IdentityReference 集合),但不包含组/角色的实际名称。我将向您展示如何获取用户所在的所有组/角色的实际名称。

首先,获取 WindowsIdentity 对象。

WindowsIdentity identity = WindowsIdentity.GetCurrent();

其次,使用 LINQ 将 SID (IdentityReference) 转换为 NTAccount。

var groups = from sid in identity.Groups select sid.Translate(typeof(NTAccount)).Value;

然后,您可以遍历这些组并将它们存储在可以在 FormsAuthenticationTicket 中使用的字符串数组中。这将为您提供 BUILTIN(本地计算机)组/角色以及用户所在的 DOMAIN 组/角色。

于 2010-01-22T21:33:33.150 回答
4

获取角色:http: //msdn.microsoft.com/en-us/library/system.web.security.roles.gettrolesforuser.aspx

使用Roles.GetRolesForUser()Roles.GetRolesForUser(Page.User.Identity.Name)获取当前用户拥有的角色数组。您可以指定要获取角色的用户Roles.GetRolesForUser("Specific UserName")

您可以使用String.Join(", ",Roles.GetRolesForUser())来获取用户拥有的角色字符串。

String.Join http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

希望这可以帮助。

于 2011-07-21T18:08:52.557 回答
2

你似乎在混合苹果和橙子。您使用的是 Windows 还是 Forms 身份验证?

无论哪种情况,您都可以从 RoleProvider 获取用户的角色(如果已实现)。

检查线程的当前主体只公开一个检查方法,如您所知,IsInRole,而角色提供者将返回用户所属角色的字符串数组。

但我不得不问你为什么要在票中加入一个角色?我能看到的唯一有效用例是您正在整合外部身份验证/角色孤岛。

如果您更全面地解释您的场景和要求,我相信我们可以找到解决您问题的具体解决方案。

于 2010-02-10T04:11:57.537 回答
1

像这样?

public static string FormsAuthUserData
{
    get
    {
        IPrincipal principal = Thread.CurrentPrincipal;
        if (principal == null) return null;
        FormsIdentity identity = principal.Identity as FormsIdentity;
        if (identity == null) return null;
        FormsAuthenticationTicket ticket = identity.Ticket;
        return ticket == null ? null : ticket.UserData;
    }
}
于 2010-01-21T06:12:28.007 回答
0

是的,Forms Authentication 似乎与 Windows 身份发生冲突,但我已经编写了一些代码,我相信这些代码可以满足您的要求。

首先,添加System.DirectoryServices对您项目的引用。

您需要先初始化一个PrincipalContext对象。

imports System.DirectoryServices

Dim userImLookingFor as AccountManagement.UserPrincipal(ctx)
Dim tempUser As New AccountManagement.UserPrincipal(ctx)
tempUser.SamAccountName = p_samAccountName
Dim searcher As New AccountManagement.PrincipalSearcher(tempUser)
If searcher.FindAll().Count = 1 Then
userImLookingFor = searcher.FindAll()(0)

当此代码运行时,userImLookingFor包含由 p_samAccountName 指定的用户。接下来,您想要获取组列表。

Dim tempGp As New AccountManagement.GroupPrincipal(userImLookingFor.Context)
Dim searcher As New AccountManagement.PrincipalSearcher(tempGp)
Dim searchResult As AccountManagement.PrincipalSearchResult(Of AccountManagement.Principal)
searchResult = searcher.FindAll()

最后,您可以参考 searchResult 集合。要获取组名称,请枚举索引并检索“用户主体名称”或“SAM 帐户名称”。

是的,Forms Authentication 在 Active Directory 中的表现不佳,但如果这有帮助,请告诉我。我不熟悉上一个答案中的方法;这两个不同的答案可能会为您提供可让您访问不同功能的对象。

于 2010-02-11T15:21:09.997 回答
0

您可以对您的用户类执行扩展方法,以获取系统中所有角色的集合(询问您的角色提供者)做一个 cicle(或使用 linq)来询问 isInRole foreach 角色并在属性中收集用户角色准备好使用。

这可以是任何类型的角色提供者的通用方式。

于 2010-02-22T21:13:05.780 回答