2012-08-28 21 views
4

我有一個WindowsIdentity,它對應於一個經過身份驗證的用戶。我怎樣才能確定身份是否與機器上的本地用戶,已添加到本機的域用戶或未添加到本機的域相對應?如何確定Windows標識是否與本地或域用戶相對應?

讓我們只說我有3個用戶帳戶:

  • DomainUser
  • LocalUser(域用戶組,沒有添加任何本地組成員)(在計算機上創建本地用戶)
  • MappedDomainUser (誰已被添加到一組機器上的域用戶)

我怎樣才能

區分
  • DomainUser和LocalUsers
  • LocalUser和MappedDomainUser
  • DomainUser和MappedDomainUser

截至目前,我取決於用戶名,如果它與機器名開始檢查。然後通過檢查用戶所屬的組(如果它是所有域用戶的一部分)進一步進行區分。不是我確定的最好方式。

因爲我有WindowsIdentity.User屬性的用戶sid,我可以用它嗎?

+0

檢查用戶名以機器名開頭是否正常,並且正常工作。您還可以檢查該用戶是否存在於域中,並比較sids以查看它是否是同一用戶。 – Dorian

+0

我在想,如果我可以使用衆所周知的SID類型和查看此用戶所屬的小島嶼發展中國家也是如此,但是WindowsIdentity.User似乎並不匹配任何衆所周知的SID。 –

+0

這是不是很清楚你想在這裏實現什麼。首先,如果域中的帳戶不是域用戶組的成員,那麼您是否仍想將其計爲域帳戶?其次,爲什麼要區分屬於任何本地組的域帳戶和不是的域帳戶? (通常你只需要檢查一些特定組的成員資格。)第三,你是否想要包含嵌套組成員? –

回答

0

假設WindowsIdentity.Name的工作原理類似Environment.UserDomainName,如果用戶名以機器名開頭,那麼它不在域上,否則它在域上。這允許你寫

public static bool IsDomain(WindowsIdentity identity) 
{ 
    string prefix = identity.Name.Split('\\')[0]; 
    if (prefix != Environment.MachineName) 
     return true; 
    else 
     return false; 
} 

的用戶域名屬性首先嚐試獲取當前用戶的Windows域名 組件帳戶名。如果 嘗試失敗,則此屬性將嘗試獲取與由UserName屬性提供的用戶名關聯的域名 。如果 因爲主機未加入到域 而導致嘗試失敗,則返回主機名稱。

對於計算機名稱和域名相同的邊緣情況,您也可以針對可用域列表(例如存儲在數據庫中)進行過濾。

+0

什麼是NETWORK_SERVICE,LOCAL SERVICE,LocalSystem, IIS APPPOOL'和 http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they-differ ### ### '的WindowsIdentity NT AUTHORITY \ NETWORK SERVICE 用戶域名:MYDOMAIN 用戶名:MYMACHINENAME $' ###### '的WindowsIdentity NT AUTHORITY \ LOCAL SERVICE 用戶域名:NT AUTHORITY 用戶名:LOCAL SERVICE' ## #### 'WindowsIdentity NT AUTHORITY \ SYSTEM 用戶域名:MYDOMAIN 用戶名:SYSTEM' ######'的WindowsIdentity IIS APPPOOL \ MyCustomAppPool 用戶域名:IIS APPPOOL 用戶名:MyCustomAppPool' – Kiquenet

5

不確定映射的域管理員。 我只是檢查用戶登錄到的域的本地和域管理員。 不要像「內建\ Admin」那樣訪問字符串,它們根據操作系統語言版本而有所不同。

我喜歡使用.net 4.5 Principals方法。 如果你可以使用4,你可以做類似的事情。5

關於問題的 我如何能夠區分

所以

  • DomainUser和LocalUsers
  • LocalUser和MappedDomainUser
  • DomainUser和MappedDomainUser之間

示例代碼

using System; 
using System.DirectoryServices.ActiveDirectory; 
using System.Security.Principal 
namespace xxxxx 
    { 
    public class UserEnvTools 
    { 

    public static bool IsDomainAdmin() 
    { //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario 
     if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null) 
      return false; 
     var domainAdmins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, 
                WindowsIdentity.GetCurrent().User.AccountDomainSid); 
     var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
     return prin != null && (prin.IsInRole(domainAdmins)); 
    } 
    public static bool IsDomainUser() 
    { 
     //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario 
     if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null) 
      return false; 

     var domainUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, 
               WindowsIdentity.GetCurrent().User.AccountDomainSid); 
     var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
     return prin != null && (prin.IsInRole(domainUsers)); 
    } 

public static bool IsLocalAdmin() 
{ 
var localAdmins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null); 
var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
return prin != null && (prin.IsInRole(localAdmins)); 
} 
    public static bool IsLocalUser() 
    { 
     var localUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); 
     var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
     return prin != null && (prin.IsInRole(localUsers)); 

    } 
    // Current security context applies 
    public static Domain GetCurrentUserDomain() 
    { 
     try 
     { 
      return System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain(); 
     } 
     // It may be better not to ctach such errors? 
     catch (ActiveDirectoryOperationException) // no Controller/AD Forest can not be contacted 
     {return null;} 
     catch (ActiveDirectoryObjectNotFoundException) // The USers Domain is not known to the controller 
     {return null;} 
    } 

    public static Domain GetCurrentMachineDomain() 
    { 
     try 
     { 
      return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain(); 
     } 
     // It may be better not to ctach such errors? 
     catch (ActiveDirectoryOperationException) // no controller or machine is not on a domain 
     { return null; } 
     catch (ActiveDirectoryObjectNotFoundException) // controller found, but the machine is not known 
     { return null; } 
    } 
+0

是,任何更清晰?我仍然不確定Mapped用戶意味着什麼。經過身份驗證的上下文在本地或在當前的域/ AD中進行身份驗證。我找不到參考文檔中的映射用戶。 http://msdn.microsoft.com/en-us/library/wwzcae1f.aspx –

+0

「映射的用戶」不是Microsoft術語,但問題將其定義爲「是本地組成員的域用戶」 。 (該定義實際上有點含糊不清,因爲它沒有說明是否包含嵌套組成員身份。) –

+0

什麼是NETWORK_SERVICE,LOCAL SERVICE,LocalSystem,IIS APPPOOL和 http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they- ###### 'WindowsIdentity NT AUTHORITY \ NETWORK SERVICE UserDomainName:MYDOMAIN 用戶名:MYMACHINENAME $' ###### '的WindowsIdentity NT AUTHORITY \ LOCAL SERVICE 用戶域名:NT AUTHORITY 用戶名:LOCAL SERVICE' ###### '的WindowsIdentity NT AUTHORITY \ SYSTEM 用戶域名:MYDOMAIN UserName:SYSTEM' ######'WindowsIdentity IIS APPPOOL \ MyCustomAppPool UserDomainName:IIS APPPOOL UserName:MyCustomAppPool' – Kiquenet

相關問題