C# 在asp.net中获取当前用户的全名
我有下面的代码,它工作得很完美,但当我在IIS中部署它时,它就不工作了C# 在asp.net中获取当前用户的全名,c#,asp.net,iis-6,C#,Asp.net,Iis 6,我有下面的代码,它工作得很完美,但当我在IIS中部署它时,它就不工作了 DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName); return de.Properties["fullName"].Value.ToString(); 任何帮助都将不胜感激。包含有关服务器上环境的信息,因此您只需获取web应用程序正在运行的进程的用户名即可
DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
return de.Properties["fullName"].Value.ToString();
任何帮助都将不胜感激。包含有关服务器上环境的信息,因此您只需获取web应用程序正在运行的进程的用户名即可。那不是你想要的
相反,您应该使用user
user.Identity.Name
来检索用户的用户名,然后您可以使用该用户名从AD获取用户名。环境。当托管在IIS上时,用户名通常返回ApplicationPool user。尝试使用HttpContext.User.Identity.Name
获取具有域的用户名
比如说,
string userName = HttpContext.User.Identity.Name;
DirectoryEntry de = new DirectoryEntry("WinNT://" + userName);
return de.Properties["fullName"].Value.ToString();
根据.Net 4.5建议,您应该始终使用ClaimsPrincipal。我给你们一个下面的程序示例,我刚刚运行了一个控制台应用程序,它运行得非常好
我在这里使用了一个扩展方法,并附加到claimsprincipal,现在您可以访问任何地方
private static void Main(string[] args)
{
var fullName = ClaimsPrincipal.Current.GetFullName();
}
public static string GetFullName(this ClaimsPrincipal principal)
{
if (!principal.HasClaim(claim => claim.Type == ClaimTypes.Surname) ||
!principal.HasClaim(claim => claim.Type == ClaimTypes.GivenName))
{
var dcUsername = principal.Identity;
// get the value from dc.
var de = new DirectoryEntry("WinNT://" + dcUsername);
var claims = new List<Claim> {new Claim(ClaimTypes.GivenName, ""), new Claim(ClaimTypes.Surname, "")};
var id = principal.Identities.First();
id.AddClaims(claims);
}
var givenName = principal.FindFirst(ClaimTypes.GivenName).Value;
var surname = principal.FindFirst(ClaimTypes.Surname).Value;
return string.Format("{0} {1}", givenName, surname);
}
private static void Main(字符串[]args)
{
var fullName=ClaimsPrincipal.Current.GetFullName();
}
公共静态字符串GetFullName(此ClaimsPrincipal主体)
{
if(!principal.HasClaim(claim=>claim.Type==ClaimTypes.姓氏)||
!principal.HasClaim(claim=>claim.Type==ClaimTypes.GivenName))
{
var dcUsername=principal.Identity;
//从dc获取值。
var de=新目录条目(“WinNT://”+dcUsername);
var claims=new List{new claims(ClaimTypes.GivenName,“”),new claims(ClaimTypes.姓氏,“”)};
var id=principal.identies.First();
id.AddClaims(索赔);
}
var givenName=principal.FindFirst(ClaimTypes.givenName).Value;
var姓氏=principal.FindFirst(ClaimTypes.姓氏).Value;
返回string.Format(“{0}{1}”,givenName,姓氏);
}
如果在域上下文中使用Windows身份验证,则此解决方案应在调试器中以及部署到IIS以获取当前用户的名/姓时都能工作。首先添加对System.DirectoryServices.AccountManagement的引用
string currentUserID = HttpContext.Current.User.Identity.Name;
using (PrincipalContext adContext = new PrincipalContext(ContextType.Domain))
{
UserPrincipal currentUser = UserPrincipal.FindByIdentity(adContext, currentUserID);
return string.Format("{0} {1}", currentUser.GivenName, currentUser.Surname);
}
到目前为止你尝试了什么?你尝试了什么?检查日志等?是否有任何错误?如果没有,结果是什么?我只想知道当前windows用户的全名,我是否需要更改IIS或我的web.config中的任何内容?只需按照以下链接中的步骤进行操作:尝试将此设置置于web.config下。另外,请确保您使用的是basic或windows auth。刚刚添加的,但当我运行它时,会出现以下错误:HTTP错误500.24-内部服务器错误检测到不适用的ASP.NET设置在集成管理管道模式下。最可能的原因:system.web/identity@impersonate设置为true。您可以尝试的事情:如果应用程序支持,请禁用客户端模拟。如果确定可以忽略此错误,则可以通过设置system.webServer来禁用此错误/validation@validateIntegratedModeConfiguration至false.“根据.Net 4.5建议”引证需要有点正确-除非它使用windows auth+impersonation.User.Identity.Name返回Null如何使用windows auth和impersonation?我在web.config中添加了以下内容:但我收到一个错误:HTTP错误500.24-内部服务器错误检测到ASP.NET设置不适用于集成托管管道模式。最可能的原因:system.web/identity@impersonate设置为true。您可以尝试的事情:如果应用程序支持,请禁用客户端模拟。如果确定可以忽略此错误,则可以通过设置system.webServer来禁用此错误/validation@validateIntegratedModeConfiguration若要为false,请在IIS中禁用匿名身份验证,启用Windows身份验证。不要为标识将其设置为模拟。假设用户位于域上并且浏览器配置正确,则应在user.Identity.Name
中获取用户名。这似乎不完整;它会导致一个未指定的错误。我认为您需要包含一些DirectorySearcher代码来查找用户并提取他们的数据。“fullName”不是DirectoryEntry.Properties的有效属性。