2016-08-29 11 views
4

をユーザーの名前を取得し、私は私はActive Directoryからユーザーの名前のみを表示する必要がActive Directoryから

lbl_Login.Text = User.Identity.Name; //the result is domain\username 

を使用しています。これは、ユーザーの本当の名前をユーザー名が表示さではなく、私は」ここで関連する他の質問と回答を確認しましたが、私は解決策を得ていません。

「User.Identity.Name」と同じプロパティがあり、ユーザーの名前のみを取得できますか?

+0

私は完全な表示名 – MethodMan

答えて

9

アクティブなディレクトリからユーザの名前が必要です。 social.msdn.microsoft.comから

string name =""; 
using (var context = new PrincipalContext(ContextType.Domain)) 
{ 
    var usr = UserPrincipal.FindByIdentity(context, User.Identity.Name); 
    if (usr != null) 
     name = usr.DisplayName; 
} 

又はこれ:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
UserPrincipal user = UserPrincipal.Current; 
string displayName = user.DisplayName; 

かであり得る:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName; 

System.DirectoryServices.AccountManagement namespaceユーザの均一なアクセスおよび操作、コンピューターを提供し、このようなコードを試します複数のプリンシパルストア間でセキュリティプリンシパルをグループ化する:Active Directoryドメインサービス(AD DS)、Active Directory (AD LDS)、およびマシンSAM(MSAM)をサポートしています。

+0

ない真@MethodManを取得するための簡単な方法を掲載します、確かにResponse.WriteまたはDebug.Writeはより一般的ですが、あなたはまだコンソール出力を使用することができます。 –

+0

ありがとう@Denis、それは働いた... ここで私はすべてのプロパティと名前空間の機能を理解するためにこれについての情報を得ることができますか? – Hans

2
using System.DirectoryServices.AccountManagement; 

string fullName = null; 
using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"hajani")) 
    { 
     if (user != null) 
     { 
      fullName = user.DisplayName; 
      lbl_Login.Text = fullName; 
     } 
    } 
} 
+0

ありがとう、これもうまくいった、私はちょうどあなたが手動で私のユーザー名を置いた場所に "User.Identity.Name"を置き換える必要があった – Hans

関連する問題