我已经标记了这个问题与C#
因为是.NET
一部分,众所周知,有些人可以编程,没有问题了解双方VB.NET
和C#
为一体,我不应该有问题,翻译C#
指令VB.NET
然后我会对用这些语言之一编写的解决方案感到满意。将用户名转换为WindowsIdentity?
我想在此功能指定的用户名来检索,如果它是一个管理员或没有什么变化,我应该怎么办?
MsgBox(UserIsAdmin("Elektro"))
' ByVal UserName as String or other needed object.
Public Function UserIsAdmin(ByVal UserName As XXXX) As Boolean
Dim Identity As Security.Principal.WindowsIdentity =
Security.Principal.WindowsIdentity.FromUserName(UserName)
Return New Security.Principal.WindowsPrincipal(Identity).
IsInRole(Security.Principal.WindowsBuiltInRole.Administrator)
End Function
PS:当然FromUserName
方法不存在。
UPDATE
我只是想@meziantou的方法,但我总是对用户名结构或有关网络条目中找不到一个例外,但是,不管怎么说,这不是确切地说我在找什么(我的意思是指定一个域或computername或其他任何不是由函数自动完成的)。
Public Class Form
Private Sub Test() Handles MyBase.Shown
' Things that I've tried:
MsgBox(UserIsAdmin("Administrador")) ' Username
MsgBox(UserIsAdmin("[email protected]")) ' [email protected]
MsgBox(UserIsAdmin(Security.Principal.WindowsIdentity.GetCurrent().Name)) ' DomainName\Username
MsgBox(UserIsAdmin("[email protected]")) ' [email protected]
MsgBox(UserIsAdmin(Security.Principal.WindowsIdentity.GetCurrent.User.Value)) ' The SID
End Sub
''' <summary>
''' Determines whether an user is an Administrator.
''' </summary>
''' <returns><c>true</c> if the user is an Administrator, <c>false</c> otherwise.</returns>
Public Function UserIsAdmin(Optional ByVal UserName As String = String.Empty) As Boolean
Dim Identity As Security.Principal.WindowsIdentity =
If(Not String.IsNullOrEmpty(UserName),
New Security.Principal.WindowsIdentity(UserName),
Security.Principal.WindowsIdentity.GetCurrent())
Return New Security.Principal.WindowsPrincipal(Identity).
IsInRole(Security.Principal.WindowsBuiltInRole.Administrator)
End Function
End Class
更新2
我尝试@Frinavale的做法,但我不能够适应这个代码来检索我感兴趣的信息。
Imports System.DirectoryServices.AccountManagement
Imports System.Security.Principal
Public Class Form1
Private Sub Test() Handles MyBase.Shown
Dim pContext As PrincipalContext = New PrincipalContext(ContextType.Machine)
Dim pUsers As Principal = New UserPrincipal(pContext)
Dim pSearcher As PrincipalSearcher = New PrincipalSearcher(pUsers)
For Each User As Principal In pSearcher.FindAll()
For Each Group As Principal In User.GetGroups
' Result of 'Administrators' (in Spanish): 'Administradores',
' so this is not efficient to compare.
MsgBox(Group.Name)
Next
' Any of these works:
' It throws an exception because
' i'm not passing the expected parameter for a WindowsIdentity.
MsgBox(UserIsAdmin(User.Name))
' MsgBox(UserIsAdmin(User.UserPrincipalName))
' MsgBox(UserIsAdmin(User.DistinguishedName))
' MsgBox(UserIsAdmin(User.SamAccountName))
Next User
End Sub
Public Function UserIsAdmin(ByVal User As String) As Boolean
Using Identity As New WindowsIdentity(User)
Return New WindowsPrincipal(Identity).IsInRole(WindowsBuiltInRole.Administrator)
End Using
End Function
End Class
您应该考虑使用PrincipalSearcher检索所有主体对象的集合(使用PrincipalSearcher。FindAll)并从那里确定哪个Principal具有您正在查找的身份。 (PrincipalSearcher信息:http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalsearcher%28v=vs.110%29.aspx)(FindAll信息: http:// msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalsearcher.findall%28v=vs.110%29.aspx) – Frinavale
@Frinavale谢谢,但我找不到适应它的方式。我试图传递给搜索者的名字给这个函数,但仍然说这个名字是不正确的。所以我试图直接在搜索器中检查用户是否是管理员,获取组“GetGroups”方法,但我看到的是“Administrators”组的名称是语言相关的,因此在我的语言中是完全的不同的名字,然后比较名称是不高效的。我能做什么?如果你愿意的话,请看我最新的问题。 thankyou – ElektroStudios
必须解决这个本地化问题。 “IsInRole”方法需要一个字符串,所以不要使用WindowsBuiltInRole.Administrator尝试传入“Administrator”(当然,我假设“Administrator”以英文存储......如果不是,比你将不得不使用资源正确地本地化单词,以便它匹配) – Frinavale