Search code examples
asp.netweb-applicationsiis-7asp.net-authenticationasp.net-authorization

Current user with ASP.NET Forms authentication app


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!


Solution

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

    Windows authentication does not use the FormsAuthenticationTicket.