获取asp.net用户名

本文关键字:用户 net asp 获取 | 更新日期: 2023-09-27 18:02:08

我在谷歌上搜索了一下,但没有一个解决方案能解决这个问题。

我的web . config:

<configuration>
    <system.web>
        <customErrors mode="Off"/>
        <compilation debug="true">
            </compilation>
      <authentication mode="Windows" />
    </system.web>
</configuration>

我的aspx页面:

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        Response.Write("Name:" + opl);
    }
</script>

我得到一个空字符串,有什么想法吗?我尝试了Request.ServerVariables,但也为用户获得了一个空字符串。

获取asp.net用户名

如果您想获得登录用户的名称,您可以使用

var user = user . identity . name

var user = user . identity . username

试试这个:

var username = User.Identity.Name

你要做的是咨询HttpContext:

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx

它将包含当前登录用户的所有信息。