在数据库中存储什么以标识Active Directory和本地帐户用户

本文关键字:用户 Directory Active 数据库 存储 什么 标识 | 更新日期: 2023-09-27 18:20:29

我正在为我的WPF应用程序编写一个权限系统,该系统需要同时使用Active Directory和本地计算机帐户;系统可以连接到网络,也可以不连接到网络。

用户可以使用AD登录名或本地计算机登录名登录到应用程序。应用程序将数据存储在SQL Server Express数据库中,每一行都有一个"所有者"。

应用程序将只显示登录用户"拥有"的行。

根据这个问题,建议存储用于标识用户的数据是LDAP objectGUID,我可以使用下面的代码检索它,并将其存储在uniqueidentifier列中。

using System.DirectoryServices.AccountManangement;
using System.Security.Principal;
public class ActiveDirectoryHelper
{
    public static Guid? GetObjectGuidFromIdentity(WindowsIdentity identity)
    {
      string domainName = identity.Name.Split('''')[0];
      PrincipalContext pc = null;
      if (domainName == Environment.MachineName)
      {
        pc = new PrincipalContext(ContextType.Machine);
      }
      else
      {
        pc = new PrincipalContext(ContextType.Domain, domainName);
      }
      UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, identity.Name);
      return user.Guid;
  }
}

但是,对于ContextType.MachineNameUserPrincipal.Guid为空。

我是否可以存储一条可以引用AD帐户或本地帐户的信息?

或者我需要添加另一列来指定目录类型(AD/SAM),并使用另一列存储不同的标识符(SID)?

在数据库中存储什么以标识Active Directory和本地帐户用户

正如您所发现的,本地帐户没有GUID。相反,他们只是有一个SID。

您可以选择使用SID来标识用户,这样您就只有一个标识符。GUID的结果是,在客户重组其Active Directory林的情况下,用户的GUID将保持静态,而SID将更改。