Current user with ASP.NET Forms authentication app

2019-08-12 17:17发布

I am trying to retrieve the current user in my web application that uses ASP.NET Forms authentication. However, System.Security.Principal.WindowsIdentity.GetCurrent().Name returns domain\windowsUser, NOT the username that was used in the FormsAuthentication.RedirectFromLoginPage method. I am using Forms authentication in my config file:

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

I am also trying to follow Microsoft's walk through and retrieve the Authentication ticket using the following snippet:

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

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

However, ident is always null because it's WindowsIdentity not FormsIdentity. What's wrong here? Thank you!

1条回答
疯言疯语
2楼-- · 2019-08-12 17:34

Use User.Identity.Name to get the user name.

Windows authentication does not use the FormsAuthenticationTicket.

查看更多
登录 后发表回答