Asp Net获取用户名

时间:2014-05-07 17:31:09

标签: c# asp.net 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但是也为用户获取了一个空字符串。

3 个答案:

答案 0 :(得分:2)

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

var user = User.Identity.Name

var user = User.Identity.UserName

答案 1 :(得分:1)

请改为尝试:

var username = User.Identity.Name

答案 2 :(得分:0)

您要做的是咨询HttpContext:

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

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

相关问题