【问题标题】:get current user full name in asp.net在asp.net中获取当前用户全名
【发布时间】:2015-01-30 01:37:20
【问题描述】:

我有下面的代码可以完美运行,但是当我在 IIS 中部署它时,它不起作用。

DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
return de.Properties["fullName"].Value.ToString();

任何帮助将不胜感激。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 你尝试了什么?检查日志等?
  • 您是否遇到任何错误?如果没有,你会得到什么结果?
  • 我只想获取当前 windows 用户的全名,是否需要更改 IIS 或 web.config 中的任何内容?
  • 只需按照以下链接中的步骤操作:stackoverflow.com/questions/24495585/…

标签: c# asp.net iis-6


【解决方案1】:

Environment 包含有关服务器环境的信息,因此您将只获得运行 Web 应用程序的进程的用户名。这不是你想要的。

相反,您应该使用User.Identity.Name 来检索用户的用户名,然后您可以使用它从 AD 中获取用户名。

【讨论】:

  • 有点正确 - 除非它使用 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 来禁用它。
  • @MinaN 在 IIS 中,禁用匿名身份验证,启用 Windows 身份验证。不要将其设置为冒充身份。假设用户在域中并且浏览器配置正确,那么您应该在User.Identity.Name 中获取用户名。
【解决方案2】:

根据 .Net 4.5 建议,您应该始终使用 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);
        }

【讨论】:

  • 尝试将此设置 放在 web.config 下
  • 还要确保您使用的是基本身份验证或 Windows 身份验证。
  • 刚刚添加了 但是当我运行它时我得到这个错误:HTTP 错误 500.24 - 内部服务器错误一个 ASP。 NET 设置已检测到不适用于集成托管管道模式。最可能的原因:system.web/identity@impersonate 设置为 true。您可以尝试的事情:如果应用程序支持它,请禁用客户端模拟。如果您确定可以忽略此错误,可以通过将 system.webServer/validation@validateIntegratedModeConfiguration 设置为 false 来禁用它。
  • “根据 .Net 4.5 建议”需要引用
【解决方案3】:

Environment.UserName 在 IIS 上托管时通常会返回 ApplicationPool 用户。尝试使用HttpContext.User.Identity.Name 获取带域的用户名。

例如,

string userName = HttpContext.User.Identity.Name;
DirectoryEntry de = new DirectoryEntry("WinNT://" + userName);
return de.Properties["fullName"].Value.ToString();

【讨论】:

  • 这似乎不完整;它会导致未指定的错误。我认为您需要包含一些 DirectorySearcher 代码来查找用户并提取他们的数据。 “fullName”不是 DirectoryEntry.Properties 的有效属性。
【解决方案4】:

如果在域上下文中使用 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多