マシンコンテキストからユーザーのフルネームを取得します。
-
19-09-2019 - |
質問
私は、私たちのイントラネット上で動作する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;
}
私がここに持っている問題は、UserPrinicipalがretrivedされたときにContextType ==マシンは、私がGIVENNAMEまたは姓などのプロパティを取得しないときということです。ユーザー(Windows Server 2008を)作成するときに、これらの値を設定するか、または私は別の方法でこれについて移動する必要があります方法はありますか?
解決
元の質問内の関数を変更する必要があります。あなたが返されるの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のプロパティは、(代わりGIVENNAMEと姓の)表示名である
所属していません StackOverflow