繁体   English   中英

ASP Net获取用户名

[英]Asp Net get username

我对此进行了Google搜索,但没有一种解决方案可以解决这种情况。

我的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但也为用户提供了一个空字符串。

如果要获取登录用户的名称,可以使用

var user = User.Identity.Name

要么

var user = User.Identity.UserName

尝试以下方法:

var username = User.Identity.Name

您要做的是查询HttpContext:

http://msdn.microsoft.com/zh-CN/library/system.web.httpcontext.user.aspx

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM