如何检查Windows帐户是否是VB 2013中的PC管理员? [英] How check if a windows account is an administrator of pc in vb 2013?

查看:68
本文介绍了如何检查Windows帐户是否是VB 2013中的PC管理员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查当前的Windows登录帐户是否是PC的管理员。

 如果My.User.IsInRole (ApplicationServices.BuiltInRole.Administrator)然后
'是管理员
其他
'不是管理员
如果


此代码可以正常工作,但是如果我用RUN AS另一个帐户对代码进行午餐,则该代码将无法正常工作,因为请使用该午餐的帐户而不是Windows帐户



使用此代码,我可以看到当前的Windows用户登录:

  Dim Coll作为ManagementObjectCollection 
Dim LogonName作为字符串
Dim GetName作为新的ManagementObjectSearcher( SELECT UserName FROM Win32_ComputerSystem)
Coll = GetName。[Get]()
LogonName = DirectCast(Coll.Cast(Of ManagementBaseObject)()。First()( UserName),String)
Dim CleanName()As String = Split(LogonName, \)

因此,在字符串LogonName中,我将具有已登录的Windows用户名帐户,但是如何检查管理员?

解决方案

 公共函数UserIsAdmin(ByVal userName As String)As Boolean 
Dim groupName As String = administrators'<-您可以本地化此
Dim isAdmin作为布尔值
使用上下文作为PrincipalContext = New PrincipalContext(ContextType.Machine)
Dim gfilter作为GroupPrincipal = GroupPrincipal.FindByIdentity(context,groupName)
如果gfilter不为空则
Dim成员= gfilter.GetMembers
每个成员中的成员
如果String.Compare(member.Name,userName,True)= 0则
isAdmin = True
如果
下一个
结束如果
使用
结束返回isAdmin
结束函数


I need to check if the current Logon Windows account is an Administrator of the PC.

    If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
        'Is Admin
    Else
        'Isn't Admin
    End If

This code work fine but if i lunch the code with RUN AS "another account" the code dont do the right job becouse take the account that lunch the code not the Windows account that is logged in.

With this code i can see the current Windows User logged in:

    Dim Coll As ManagementObjectCollection
    Dim LogonName As String
    Dim GetName As New ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")
    Coll = GetName.[Get]()
    LogonName = DirectCast(Coll.Cast(Of ManagementBaseObject)().First()("UserName"), String)
    Dim CleanName() As String = Split(LogonName, "\")

So in the string LogonName i'll have the Windows user name account that is logged in but how can i check if is an Administrator?

解决方案

Public Function UserIsAdmin(ByVal userName As String) As Boolean
        Dim groupName As String = "administrators" '<--You can localize this'
        Dim isAdmin As Boolean
        Using context As PrincipalContext = New PrincipalContext(ContextType.Machine)
            Dim gfilter As GroupPrincipal = GroupPrincipal.FindByIdentity(context, groupName)
            If gfilter IsNot Nothing Then
                Dim members = gfilter.GetMembers
                For Each member In members
                    If String.Compare(member.Name, userName, True) = 0 Then
                        isAdmin = True
                    End If
                Next
            End If
        End Using
        Return isAdmin
End Function

这篇关于如何检查Windows帐户是否是VB 2013中的PC管理员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆