【问题标题】:IsInRole Getting New Security TokenIsInRole 获取新的安全令牌
【发布时间】:2017-01-15 06:30:22
【问题描述】:

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

此代码第一次在特定计算机上运行时,操作系统会为指定的用户生成身份令牌。然后 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...

【问题讨论】:

    标签: c# vb.net windows-identity isinrole windows-principal


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      不确定这是否能解决您的问题,请尝试直接或间接调用 WindowsIdentity 类的 dispose 方法。

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

      【讨论】:

      • 不幸的是它没有任何影响。从它的行为来看,我认为安全令牌是由操作系统存储的,而 WindowsPrinciple 基本上只是一个指向该对象的指针。 (WindowsPrinciple 没有要调用的析构函数)因此它在应用程序的执行之间持续存在。希望我知道如何清除缓存。
      • 原来我错了。它正在缓存,但我不确定它是否在操作系统上——它可能在 AD 端。最终,当我创建一个新的 identityWindowsPrincipal 时,它会更新(正确)组成员身份。
      • 在这种情况下,除了在查询 AD 时直接查询 AD 之外,可能没有可用的解决方案以编程方式获得更新的结果 - 不像这些安全令牌并不总是最新的。不幸的是,查询 AD 很慢——至少在我们的环境中是这样。
      • 继续前进,将 50 点赏金授予您 Sridhar,因为我感谢您加入并提出建议。
      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 2011-01-11
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 2021-10-20
      • 1970-01-01
      相关资源
      最近更新 更多