2

イントラネット上で実行されるASP.NETアプリケーションがあります。本番環境では、ドメインコンテキストからユーザーを取得し、名前と名前(UserPrincipal.GivenNameおよびUserPrincipal.Surname)を含む多くの情報にアクセスできます。

私たちのテスト環境は本番ドメインの一部ではなく、テストユーザーはテスト環境にドメインアカウントを持っていません。そこで、それらをローカルマシンユーザーとして追加します。スタートページを参照すると、資格情報の入力を求められます。次のメソッドを使用してUserPrincipalを取得します

public static UserPrincipal GetCurrentUser()
        {
            UserPrincipal up = null;

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
            }

            if (up == null)
            {
                using (PrincipalContext context = new PrincipalContext(ContextType.Machine))
                {
                    up = UserPrincipal.FindByIdentity(context, User.Identity.Name);
                }
            }

            return up;
        }

ここでの問題は、ContextType == MachineのときにUserPrinicipalが取得されたときに、GivenNameやSurnameなどのプロパティを取得できないことです。ユーザー(Windows Server 2008)を作成するときにこれらの値を設定する方法はありますか、それとも別の方法でこれを行う必要がありますか?

4

1 に答える 1

4

元の質問の関数を変更する必要があります。返された UserPrincipal オブジェクトにアクセスしようとすると、ObjectDisposedException が発生します。

また、User.Identity.Name は使用できないため、渡す必要があります。

上記の関数に次の変更を加えました。

public static UserPrincipal GetUserPrincipal(String userName)
        {
            UserPrincipal up = null;

            PrincipalContext context = new PrincipalContext(ContextType.Domain);
            up = UserPrincipal.FindByIdentity(context, userName);

            if (up == null)
            {
                context = new PrincipalContext(ContextType.Machine);
                up = UserPrincipal.FindByIdentity(context, userName);
            }

            if(up == null)
                throw new Exception("Unable to get user from Domain or Machine context.");

            return up;
        }

さらに、使用する必要がある UserPrincipal のプロパティは DisplayName (GivenName と Surname ではなく) です。

于 2009-10-02T17:39:34.227 に答える