简体   繁体   English

IsInRole获得新的安全令牌

[英]IsInRole Getting New Security Token

I'm using WindowsPrincipal's IsInRole method to check group memberships in WPF and Winforms apps. 我正在使用WindowsPrincipal的IsInRole方法来检查WPF和Winforms应用程序中的组成员身份。 I'm generating an identity token which can be for any AD user (not necessarily the user who's actually logged into the computer--depending on what I'm doing I don't necessarily authenticate, I just use the basic informational level token (I think the proper name for it is "identity token"). 我正在生成一个身份令牌,可以用于任何AD用户(不一定是实际登录到计算机的用户 - 取决于我正在做什么,我不一定要进行身份验证,我只使用基本的信息级令牌(我认为它的正确名称是“身份令牌”)。

The first time this code is run on a particular computer the operating system generates the identity token for the user specified. 第一次在特定计算机上运行此代码时,操作系统会为指定的用户生成标识令牌。 That token is then used by the IsInRole function to validate group memberships. 然后,IsInRole函数使用该标记来验证组成员身份。 It's fast so I really like it. 它很快,所以我非常喜欢它。 However, subsequent calls to create the WindowsIdentity/WindowsPrincipal reference the existing token instead of creating a new one. 但是,后续调用创建WindowsIdentity / WindowsPrincipal会引用现有令牌而不是创建新令牌。 The only way I know how to update the token is to log out of the computer or reboot (which clears the token cache). 我知道如何更新令牌的唯一方法是退出计算机或重新启动(清除令牌缓存)。 Does anyone know a better way to reset cached identity tokens? 有谁知道重置缓存身份令牌的更好方法?

Example Code C#: 示例代码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: 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...

Not sure if this may resolve your issue, try calling the dispose method of WindowsIdentity class either directly or indirectly. 不确定这是否可以解决您的问题,请尝试直接或间接调用WindowsIdentity类的dispose方法。

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

Turns out I was wrong. 事实证明我错了。 It is caching, but it appears to be on the AD side. 它是缓存,但它似乎在AD方面。 Eventually after I create a new identityWindowsPrincipal it gets updated to the correct group memberships. 最后,在我创建一个新的identityWindowsPrincipal后,它会更新为正确的组成员身份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM