我当前的项目要求我针对Active Directory组验证用户.问题是,计算机可能并不总是连接到域,但用户可能仍需要运行该工具.我知道我没有连接时无法查询Active Directory,而是在尝试查询机器SAM(MSAM).
我在断开网络连接时无法确定当前用户.这是我正在使用的:
PrincipalContext principalctx = new PrincipalContext(ContextType.Machine);
UserPrincipal uprincipal = new UserPrincipal(principalctx);
Run Code Online (Sandbox Code Playgroud)
从这一点开始,我如何询问当前谁登录到本地计算机.
当连接到域时,我可以使用查询UserPrincipal.Current.如果我没有连接到域,它将无法说"The server could not be contacted".注意:使用上面的代码不能使用此方法,而是可以放弃PrincipalContext并直接查询当前用户.在识别出当前用户的情况下,我可以查询GetGroups()并确定它们是否在所需的组中.
此外,有人可以描述这三个ContextType选项ApplicationDirectory, Domain, Machine.我担心我不完全理解每个选项,因此可能使用不正确.
我正在使用带有DataTemplate如下所示的列表框(xaml 简化和变量名称已更改)。
<ListBox ItemsSource="{Binding Path=ObservCollectionItems}"
SelectedItem="{Binding Path=SelectedItemVar, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding SomeVar}" />
<Border>
<StackPanel>
<Button Content="String1"
Command="{Binding DataContext.Command1}
RelativeSource={RelativeSource FindAncestor, ListBox, 1}}" />
<Button Content="String2"
Command="{Binding DataContext.Command2}
RelativeSource={RelativeSource FindAncestor, ListBox, 1}}" />
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
<ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
当我单击其中一个按钮时,我需要 SelectedItemVar(依赖属性)进行更新。SelectedItemVar 然后用于相应按钮的命令。SelectedItemVar 在单击TextBlock或时会更新Border,但在单击任一按钮时不会更新。我在这里找到了解决此问题的非 MVVM 解决方案。我不想在文件隐藏中添加代码来解决这个问题,就像他们在链接中所做的那样。
是否有可以在 XAML 中完成的干净解决方案。除了非 MVVM 解决方案,我还没有发现任何人有这个问题。我会认为这是相当普遍的。
最后,我Command="{Binding DataContext.CommandName} RelativeSource={RelativeSource FindAncestor, ListBox, 1}为命令绑定找到了这个。我不完全明白它在做什么,但我知道当我直接绑定到CommandName.