Current user with ASP.NET Forms authentication app

北城以北 提交于 2019-12-11 13:17:21

问题


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:


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

Windows authentication does not use the FormsAuthenticationTicket.



来源:https://stackoverflow.com/questions/16468841/current-user-with-asp-net-forms-authentication-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!