如何限制 VB.Net 中的某些设置?

How to restrict only certain settings in VB.Net?

我正尝试在 VB.Net 中编写程序以提供给我组织中的某些员工。它将让他们管理 AD 的某些方面,但仅限于他们被授权访问的内容。这部分有效。

让我感到困惑的部分是如何将程序中的设置锁定为仅由特定组(即域管理员)允许,而不仅仅是本地管理员(即通过使用 UAC)。这些设置当前存储在 My.Settings.* 中,并且是用户范围的设置(因此它们可以在以后更改)。

理想情况下,我希望出现 UAC 样式的对话框,但要针对 Active Directory 组而不是本地系统的管理员组进行身份验证,然后用户可以更改设置。该程序不会总是由域管理员 运行,但如果输入凭据,则需要更改设置的选项。这可能吗?

获取当前登录用户的组相当容易。有几种不同的方法,但这可能是最简单的方法:

UserPrincipal.Current.GetAuthorizationGroups()

这将递归地获取用户所属的所有安全组 - 因此,如果用户是组 A 的成员,并且该组在组 B 中,您将在此列表中看到组 B。 (我实际上写了一篇文章,讨论了该方法的工作原理:Finding all of a user’s groups

然后您只需查看列表中的 Name 属性 组即可检查您想要的组是否存在。

如果您觉得这样做太慢,可以使用一种速度更快但不太友好的方法。用户的身份验证令牌包含其组的所有 SID(这实际上是 GetAuthorizationGroups() 使用的),但它 包含 SID。所以 GetAuthorizationGroups() 将去 AD 并获取每个属性(每组一个网络请求)。

为避免网络流量(和时间),您可以使用以下方法直接查看 SID:

System.Security.Principal.WindowsIdentity.GetCurrent().Groups

那个returns一个列表SecurityIdentifier。如果您存储您正在使用的组的 SID,而不是名称,那么您可以将它与 SecurityIdentifierValue 进行比较,并且您可以在完全不联系 AD 的情况下完成这一切。

更新: 所有这些都将帮助您确定用户是否在您想要的组中(域管理员?)。如果您想为用户提供输入不同凭据的选项,那么......您可以通过多种方式实现这一点。

我认为最简单的方法是创建您自己的接受用户名和密码的对话框,然后使用 solution here 使用新凭据重新启动您的应用程序(转换为 VB.NET):

Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()

proc.StartInfo.UseShellExecute = False
proc.StartInfo.FileName = Application.ExecutablePath
proc.StartInfo.Domain = "domainname"
proc.StartInfo.UserName = "username"

'Translate the user-entered String into a SecureString
'If you take the password as a SecureString in the first place
'you won't have to do this
Dim password As String = "user entered password"
Dim ssPwd As System.Security.SecureString = New System.Security.SecureString()
For x As Integer = 0 To password.Length - 1
    ssPwd.AppendChar(password(x))
Next
proc.StartInfo.Password = ssPwd

proc.Start()
Application.Exit()

请注意 documentation on the UserName property 表示:

If you use the UPN format, user@DNS_domain_name, the Domain property must be null.

因此您可能必须检查用户是否给了您 DOMAIN\UsernameUsername@domain.com 并适当地分配 UserNameDomain 属性。