18

プロパティはUser.Identity.NameドメインログインIDを返します。

どのクラス/プロパティが実際のユーザー名を公開しますか?

my_domain\jdoeを提供するWebアプリケーションにログインするユーザー「JohnDoe」の場合

**User.Identity.Name -** 
Returns : *my_domain\jdoe*

**System.Environment.UserName**
Returns: *jdoe*

どのクラス/プロパティが返されますか?... "ジョン・ドウ"

4

5 に答える 5

18

Active Directoryを検討している場合は、指定されたsamAccountNameに対応するUserPrincipalを見つけて、そこからDisplayNameプロパティを取得する必要があります。設定されていない場合がありますのでご注意ください。

string fullName = null;
using (PrincipalContext context = new PrincipalContext( ContextType.Domain ))
{
    using (UserPrincipal user
            = UserPrincipal.FindByIdentity( context,
                                            User.Identity.Name ))
    {
        if (user != null)
        {
            fullName = user.DisplayName;
        }
    }
}
于 2009-01-30T00:10:29.030 に答える
4

ログイン名の代わりに、ActiveDirectoryユーザーアカウントの表示名の後にいるように聞こえます。AD検索(DirectorySearcher)を実行し、検索結果プロパティから表示名を取得することをお勧めします。

質問adsiにタグを付けたので、AD環境にいると想定しています。

注: .NET 3.5を使用している場合は、tvanfossonの投稿を参照することをお勧めします。

于 2009-01-30T00:09:00.830 に答える
1

IIdentityインターフェースは、User.IdentityにNameプロパティを提供するインターフェースです。IIdentityインターフェイスは、データストア(SQL Server、Active Directoryなど)からユーザーを検索する方法を知っている任意の数のクラスに実装できます。

「JohnDoe」を提供するIIdentityインターフェースのプロパティはありません。その情報がデータストアにある場合は、そのデータストアに固有のツールを使用してアクセスする必要があります。

とはいえ、User.Identityによって返されるオブジェクトに、IIdentity以外のインターフェイスからアクセスできる「JohnDoe」を含むプロパティがある可能性は十分にあります(たとえば、カスタムIIdentity実装がこれを行います)。

于 2009-01-30T00:06:13.220 に答える
1
using System.DirectoryServices;


public static string GetFullName(string strLogin)
    {
        string str = "";
        string strDomain;
        string strName;

        // Parse the string to check if domain name is present.
        int idx = strLogin.IndexOf('\\');
        if (idx == -1)
        {
            idx = strLogin.IndexOf('@');
        }

        if (idx != -1)
        {
            strDomain = strLogin.Substring(0, idx);
            strName = strLogin.Substring(idx + 1);
        }
        else
        {
            strDomain = Environment.MachineName;
            strName = strLogin;
        }

        DirectoryEntry obDirEntry = null;
        try
        {
            obDirEntry = new DirectoryEntry("WinNT://" + strDomain + "/" + strName);
            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
            object obVal = coll["FullName"].Value;
            str = obVal.ToString();
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return str;
    }

そして、あなたはただ呼び出すことができます

var strJonDoeName = GetFullName(User.Identity.Name)

ここからコードモック

于 2009-01-30T00:14:57.150 に答える
1

どこかで間違いを犯したのかもしれませんが、WinNT://... はドメイン アカウントに対して機能しませんでした。さらに、ユーザーがマシンと同じドメインにない場合、PrincipalContext は目的の要素を見つけられない可能性があります (上記のように使用すると、現在のコンテキストで検索されるため)。

以下は、User.Identity.Name によって提供される「わかりやすいドメイン名」を LDAP 準拠のドメインに変換する必要があります。ドメインのdistinctNameを使用すると、必要なldapパス情報が提供されます(これにより、クロスドメインの問題が解決されました):

(VB.NET, sorry)
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory
...

Dim strUserName As String
Dim objDirContext As DirectoryContext
Dim objContext As PrincipalContext
Dim objPrincipal As Principal
Dim strLDAPDomainPath As String
...

// User.Identity.Name delivers domain\account or account@domain
// Split User.Identity.Name in domain and account as specified above
strDomain = "my_domain"
strAccount = "jdoe"

// Get LDAP domain relative path (distinguishName) from short domain name (e.g. my_domain -> us.my_domain.com -> DC=us,DC=my_domain,DC=com)
Try
    objDirContext = New DirectoryContext(DirectoryContextType.Domain, strDomain)
    strLDAPDomainPath = Domain.GetDomain(objDirContext).GetDirectoryEntry.Properties("distinguishedName").Value.ToString
Catch objException As DirectoryServicesCOMException
    Throw New Exception("Couldn't get LDAP domain: " & objException.Message)
End Try

// Find user in LDAP
// Nothing results in using current domain controller
Try
   objContext = New PrincipalContext(ContextType.Domain, Nothing, strLDAPDomainPath)
   objPrincipal = Principal.FindByIdentity(objContext, IdentityType.Name, strAccount)

   If Not IsNothing(objPrincipal) Then
      strUserName = objPrincipal.DisplayName
   End If
Catch objException As Exception
   Throw New Exception("Couldn't get user display name: " & objException.Message)
End Try

// strUserName should now contain the wanted full name
于 2010-01-19T18:28:56.223 に答える