2015-11-04 35 views
0

我正在爲需要與Active Directory和本地計算機帳戶一起工作的WPF應用程序編寫權限系統;該系統可能連接到或可能不連接到網絡。在數據庫中存儲什麼以識別Active Directory和本地帳戶用戶

用戶可以使用AD登錄或本地計算機登錄登錄到應用程序。應用程序將數據存儲在SQL Server Express數據庫中,並且每行都會有一個「所有者」。

該應用程序將只顯示登錄的用戶「擁有」的行。

根據this question,存儲以識別用戶的推薦數據是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; 

    } 
} 

然而,UserPrincipal.Guid是空的ContextType.MachineName

我可以存儲的單個信息是否可以引用AD帳戶或本地帳戶?

或者我需要添加另一列來指定目錄類型(AD/SAM)並使用另一列來存儲不同的標識符(SID)?

回答

0

正如您發現的,本地帳戶沒有GUID。相反,他們只是有一個SID。

您可以選擇使用SID來標識用戶,因此您只有一個標識符。 GUID的結果是,在客戶重構其Active Directory林的情況下,用戶的GUID將保持靜態,同時SID將發生變化。

相關問題