4

認証されたユーザーに対応するWindowsIdentityがあります。IDがマシンのローカルユーザー、マシンに追加されたドメインユーザー、またはマシンに追加されていないドメインに対応しているかどうかを確認するにはどうすればよいですか?

私が3つのユーザーアカウントを持っているとしましょう:

  • DomainUser(ドメインユーザーグループのメンバー、ローカルグループには追加されません)
  • LocalUser(マシン上に作成されたローカルユーザー)
  • MappedDomainUser(マシン上のグループに追加されたドメインユーザー)

どうすれば区別できますか

  • DomainUserとLocalUsers
  • LocalUserとMappedDomainUser
  • DomainUserとMappedDomainUser

今のところ、私はユーザー名に依存していて、それがマシン名で始まるかどうかを確認しています。次に、ユーザーが属しているグループを確認することで、さらに区別します(すべてのドメインユーザーの一部である場合)。私が確信している最善の方法ではありません。

WindowsIdentity.Userプロパティのユーザーsidを持っているので、どういうわけかそれを使用できますか?

4

2 に答える 2

8

マップされたドメイン管理者についてはよくわかりません。ユーザーがログインしているドメインのローカルおよびドメイン管理者を確認するだけです。OS言語のバージョンによって異なる「builtin\Admin」のような文字列にはアクセスしないでください。

私は.net4.5プリンシパルアプローチを使用するのが好きです。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; }
    }
于 2012-09-02T14:18:46.810 に答える
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;
}

UserDomainName プロパティは、最初に現在のユーザーの Windows アカウント名のドメイン名コンポーネントを取得しようとします。その試行が失敗した場合、このプロパティは、UserName プロパティによって提供されるユーザー名に関連付けられたドメイン名の取得を試みます。ホスト コンピューターがドメインに参加していないためにその試行が失敗した場合は、ホスト コンピューター名が返されます。

コンピュータ名とドメイン名が同じであるというエッジケースのために、利用可能なドメイン (DB に保存されているものなど) のリストに対してフィルタリングすることもできます。

于 2012-08-28T13:45:58.010 に答える