【问题标题】:Get username with window authentication通过窗口身份验证获取用户名
【发布时间】:2019-03-08 14:28:58
【问题描述】:

我们需要用 C# 实现一个代码,用于 MVC 应用程序的窗口身份验证。就像当用户尝试访问一个 URL 时,他/她会弹出身份验证窗口,用户将输入他/她的用户名和密码,我们需要在 C# 中获取用户名并将其存储到数据库中。

我可以使用下面的代码检索用户名,但只有在我托管 n 服务器后在本地主机上托管应用程序时,我才能将池标识作为用户名。

System.Security.Principal.WindowsIdentity.GetCurrent().Name;

宿主应用程序后如何获取?

【问题讨论】:

  • System.Windows.Forms 命名空间中的 SystemInformation 类可以为您提供当前登录的用户名。这是您想要的吗?
  • 什么样的应用程序? ASP.NET 应用应使用Page.User 或Controller.User。
  • @LexLi 这是一个 MVC 应用程序。

标签: c# iis model-view-controller


【解决方案1】:
【解决方案2】:

如果您想在启用 Windows 身份验证时获取 MVC 中的用户名,我建议您可以使用 User.Identity.Name 而不是 System.Security.Principal.WindowsIdentity.GetCurrent().Name。

您可以编写如下测试:

    public ActionResult Index()
    {
        ViewBag.PrincipalName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        ViewBag.Name = User.Identity.Name;
        return View();
    }

查看:

<br />
The Principalname is :  @ViewBag.PrincipalName 
<br />
The name is :  @ViewBag.Name

结果:

【讨论】:

  • User.Identity.Name,为此我得到的是来宾用户名,而不是用户在窗口身份验证弹出框中键入的任何内容。
  • 据我所知,windows认证的User.Identity.Name是域和用户名。通常,客户端使用域/用户名登录您的应用程序。你能告诉使用什么是访客用户名吗?你能分享详细的 webconfig 设置吗?我们将更容易重现问题并找到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多