使用 ASP.NET Forms 身份验证应用程序的当前用户

2024-03-04

我正在尝试检索使用 ASP.NET Forms 身份验证的 Web 应用程序中的当前用户。 然而,System.Security.Principal.WindowsIdentity.GetCurrent().Name返回域\windows 用户,而不是在中使用的用户名FormsAuthentication.RedirectFromLoginPage方法。 我在配置文件中使用表单身份验证:

<authentication mode="Forms">
      <forms loginUrl="Views/Login.aspx" name=".ASPXFORMSAUTH" timeout="1" cookieless="UseUri">
      </forms>
    </authentication>
    <authorization>
      <deny users="?" />
    </authorization>

我还尝试按照 Microsoft 的演练并使用以下代码片段检索身份验证票证:

    if (Request.IsAuthenticated)
    {
         var ident = User.Identity as FormsIdentity;
        if (ident != null)
        {

            FormsAuthenticationTicket ticket = ident.Ticket;
            var name = ticket.Name;
        }
    }

但是,ident 始终为 null,因为它是 WindowsIdentity 而不是 FormsIdentity。这是怎么回事? 谢谢你!


Use User.Identity.Name获取用户名。

Windows 身份验证不使用FormsAuthenticationTicket.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 ASP.NET Forms 身份验证应用程序的当前用户 的相关文章

随机推荐