2016-09-07 122 views
6

我使用WindowsPrincipal的IsInRole方法来检查WPF和Winforms应用程序中的组成员身份。我生成的身份令牌可以是任何AD用户(不一定是实际登录到计算机的用户 - 取决于我在做什么,我不一定要进行身份验证,我只是使用基本的信息级别令牌(我认为它的正确名称是“身份令牌”)IsInRole获取新的安全令牌

第一次在特定的计算机上运行此代码时,操作系统会为指定的用户生成身份令牌,然后该令牌由IsInRole函数使用来验证组成员身份,它很快,所以我非常喜欢它,但是,后来创建WindowsIdentity/WindowsPrincipal的调用引用了现有的令牌,而不是创建一个新的令牌。我知道如何更新令牌的唯一方法是退出计算机或重新启动(清除令牌缓存)有谁知道更好的方法来重置缓存的身份令牌?

示例代码C#:

Using System.Security.Principal; 
WindowsIdentity impersonationLevelIdentity = new WindowsIdentity("Some_UserID_That_Isn't_Me", null); 
WindowsPrincipal identityWindowsPrincipal = new WindowsPrincipal(impersonationLevelIdentity); 
If (identityWindowsPrincipal.IsInRole("AN_AD_GROUP")) { ... 

VB:

如果
Imports System.Security.Principal 
Dim impersonationLevelIdentity = New WindowsIdentity("Some_UserID_That_Isn't_Me", Nothing) 
Dim identityWindowsPrincipal = New WindowsPrincipal(impersonationLevelIdentity) 
if identityWindowsPrincipal.IsInRole("AN_AD_GROUP") then... 

回答

0

原来我错了。它是缓存,但它似乎在AD方面。最终在创建新的identityWindowsPrincipal后,它会更新为正确的组成员身份。

0

不知道这可能会解决你的问题,请尝试直接或间接调用的WindowsIdentity类的Dispose方法。

using (WindowsIdentity impersonationLevelIdentity = new WindowsIdentity("Some_UserID_That_Isn't_Me", null)) 
{ 
    // your code 
} 
+0

不幸的是它没有任何影响。从它的行为我认为安全令牌正在被操作系统存储,Windows原则基本上只是一个指向该对象的指针。 (WindowsPrinciple没有析构函数调用)因此它在应用程序的执行之间持续存在。希望我知道如何清除缓存。 – Jeff

+0

原来我错了。它是缓存,但我不确定它是否在操作系统上 - 它可能在AD一侧。最终,当我创建一个新的identityWindowsPrincipal时,它会更新(正确)组成员资格。 – Jeff

+0

在这种情况下,可能没有可用的编程解决方案,除非在查询AD时直接查询AD以获得更新结果 - 与这些安全令牌并不总是最新的不同。不幸的是,查询AD的速度很慢 - 至少在我们的环境中。 – Jeff