使用ASP.NET当前用户窗体身份验证的应用程序 [英] Current user with ASP.NET Forms authentication app

查看:276
本文介绍了使用ASP.NET当前用户窗体身份验证的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索当前用户在使用ASP.NET窗体身份验证我的Web应用程序。
然而, System.Security.Principal.WindowsIdentity.GetCurrent()。名称返回域\\ windowsUser,而不是用户名即是在 FormsAuthentication.RedirectFromLoginPage使用方法。
我使用Forms身份验证在我的配置文件:

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;
        }
    }

不过,Ident是始终为空,因为它的WindowsIdentity不FormsIdentity。什么是错在这里?
谢谢!

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

推荐答案

使用 User.Identity.Name 来获取用户名。

Windows身份验证不使用的FormsAuthenticationTicket

Windows authentication does not use the FormsAuthenticationTicket.

这篇关于使用ASP.NET当前用户窗体身份验证的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆