User.Identity.Name
属性返回域登录id。
哪个类/属性公开了实际的用户名?
对于登录到提供my_domain\jdoe的web应用程序的用户"John Doe“
**User.Identity.Name -**
Returns : *my_domain\jdoe*
**System.Environment.UserName**
Returns: *jdoe*
哪个类/属性返回?...“无名氏”
发布于 2009-01-30 00:10:29
如果您正在考虑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-30 00:06:13
IIdentity接口是在User.Identity上提供Name属性的接口。IIdentity接口可以在任意数量的知道如何从数据存储(SQL Server、Active Directory等)中查找用户的类上实现。
没有提供"John Doe“的IIdentity接口的属性。如果该信息位于您的数据存储中,则需要使用特定于该数据存储的工具来访问它。
也就是说,完全有可能由User.Identity返回的对象具有一个包含"John Doe“的属性,您可以通过IIdentity以外的其他接口访问该属性(例如,我们的自定义IIdentity实现可以做到这一点)。
发布于 2009-01-30 00:14:57
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)
代码从here模拟它
https://stackoverflow.com/questions/494006
复制相似问题