获取字符串变量中用户的角色
有没有一种方法可以使用以下命令获取字符串变量中的角色...
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();
但这是不对的,
类似于这样的东西......
is there a way i can get the role in a string variable using the below commands....
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal wp = new System.Security.Principal.WindowsPrincipal(wi);
i need this for
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, // version
UserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(60),// Expiration
false, // Persistent
role); // User data
as string role= wp.IsInRole();
but this is not right
something similar to this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以从 WindowsIdentity.Groups 属性获取用户所属的组/角色列表。 WindowsIdentity.Groups 集合仅包含用户所在组/角色的 SID(IdentityReference 集合),但不包含组/角色的实际名称。我将向您展示如何获取用户所在的所有组/角色的实际名称。
首先,获取 WindowsIdentity 对象。
其次,使用 LINQ 将 SID (IdentityReference) 转换为 NTAccount。
然后,您可以循环遍历这些组并将它们存储在可在 FormsAuthenticationTicket 中使用的字符串数组中。这将为您提供内置(本地计算机)组/角色以及用户所在的域组/角色。
You can get a list of groups/roles that a user is part of from the WindowsIdentity.Groups property. The WindowsIdentity.Groups collection only contains the SID's (collection of IdentityReference) of the groups/roles a user is in, but not the actual names of the groups/roles. I will show you how to get the actual names of all the groups/roles a user is in.
First, get the WindowsIdentity object.
Second, use LINQ to translate the SID's (IdentityReference) to NTAccount's.
You can then loop through the groups and store them in a string array that can be used in the FormsAuthenticationTicket. This will get you both the BUILTIN (local computer) groups/roles and also DOMAIN groups/roles the user is in.
要获取角色: http://msdn.microsoft .com/en-us/library/system.web.security.roles.getrolesforuser.aspx
使用
Roles.GetRolesForUser()
或Roles.GetRolesForUser(Page.User.Identity) .Name)
获取当前用户拥有的角色数组。您可以通过Roles.GetRolesForUser("Specific UserName")
指定要获取哪个用户的角色。您可以使用
String.Join(", ",Roles.GetRolesForUser())< /code> 获取用户拥有的角色字符串。
String.Join
http://msdn.microsoft.com /en-us/library/57a79xd0.aspx希望这有帮助。
For getting Roles: http://msdn.microsoft.com/en-us/library/system.web.security.roles.getrolesforuser.aspx
Use
Roles.GetRolesForUser()
orRoles.GetRolesForUser(Page.User.Identity.Name)
to get an array of the roles the current user has. You can specify which user you want to get the roles for byRoles.GetRolesForUser("Specific UserName")
You can use
String.Join(", ",Roles.GetRolesForUser())
to get a string of roles the user has.String.Join
http://msdn.microsoft.com/en-us/library/57a79xd0.aspxHope this helps.
你似乎在混合苹果和橙子。您使用的是 Windows 还是 Forms 身份验证?
无论哪种情况,您都可以从 RoleProvider 获取用户的角色(如果已实现)。
检查线程的当前主体只会公开一个检查方法,如您所知,IsInRole,而角色提供程序将返回用户所属角色的字符串数组。
但我想问一下为什么你要把一个或多个角色打包到票中?我能看到的唯一有效的用例是您正在整合外部身份验证/角色孤岛。
如果您更全面地解释您的场景和要求,我相信我们可以找到针对您问题的具体解决方案。
You seem to be mixing apples and oranges. Are you using Windows or Forms authentication?
In either case, you can get the user's roles from the RoleProvider, if it is implemented.
Examining the thread's current principal only exposes a check method, as you know, IsInRole, whereas the role provider will return a string array of roles the user belongs to.
But I have to ask why you are packing a role(s) into the ticket? The only valid use case I can see for this is you are consolidating external auth/role silos.
If you explain your scenario and requirements a bit more fully I am sure we can find a specific solution to your problem.
像这样吗?
Like so?
是的,表单身份验证似乎与 Windows 身份冲突,但我已经编写了一些代码,我相信它们可以满足您的要求。
首先,将对
System.DirectoryServices
的引用添加到您的项目中。您需要首先初始化一个
PrincipalContext
对象。导入 System.DirectoryServices
Dim userImLookingFor 作为 AccountManagement.UserPrincipal(ctx)
将 tempUser 调暗为新 AccountManagement.UserPrincipal(ctx)
tempUser.SamAccountName = p_samAccountName
将搜索器调暗为新 AccountManagement.PrincipalSearcher(tempUser)
如果 searcher.FindAll().Count = 1 则
userImLookingFor = searcher.FindAll()(0)
当此代码运行时,
userImLookingFor
包含 p_samAccountName 指定的用户。接下来,您想要获取组的列表。将 tempGp 调暗为新 AccountManagement.GroupPrincipal(userImLookingFor.Context)
将搜索器调暗为新 AccountManagement.PrincipalSearcher(tempGp)
将 searchResult 设为 AccountManagement.PrincipalSearchResult(Of AccountManagement.Principal)
searchResult = searcher.FindAll()
最后,您可以参考searchResult集合。要获取组名称,请枚举索引并检索“用户主体名称”或“SAM 帐户名称”。
是的,表单身份验证与 Active Directory 的配合不太好,但请告诉我这是否有帮助。我对之前答案中的方法不熟悉;这两个不同的答案可能会给您提供可以访问不同功能的对象。
Yes, Forms Authentication seems to clash with Windows Identities, but I have written some code which I believe will do what you ask.
First of all, add a reference to
System.DirectoryServices
to your project.You need to initialize a
PrincipalContext
object first.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)
When this code runs,
userImLookingFor
contains the user specified by p_samAccountName. Next, you want to get a list of the groups.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()
Finally, you may refer to the searchResult collection. To get the group names, enumerate through the indexes and and retrieve either the "User Principal Name" or the "SAM Account Name".
Yup, Forms Authentication does not play that well with Active Directory, but let me know if this helps. I am not familiar with the approach in the previous answer; these two different answers may give you objects that give you access to different functionality.
您可以对您的用户类执行扩展方法,以获取系统中所有角色的集合(询问您的角色提供者)执行 cicle (或使用 linq)来询问 isInRole foreach 角色并收集准备好的属性中的用户角色使用。
这可以是任何类型的角色提供者通用的方式。
You can do a extension method to your user class, to get the collection of all Roles in the system (asking your Role provider) do a cicle (or use linq) to ask the isInRole foreach role and collect the user Roles in a property ready to use.
That can be a way generic to any type of Role provider.