当我在IIS中启用Windows身份验证时,当前登录的用户名显示在aspx网页上

时间:2019-01-23 09:32:55

标签: asp.net iis windows-authentication

我正在创建一个网页(aspx),以在用户尝试访问该页面时获取当前登录的用户信息。

protected void Page_Load(object sender, EventArgs e)
{
    userName = User.Identity.Name.Split('\\')[1];
}

所以我在这里正确地获得了用户名(域\用户名),但是我面临的问题是,这个用户名也在表单顶部也在网页上显示。我不想要的。 我没有使用任何Response.Write或Console.Write在网页上显示它。

我认为我在web.config中缺少一些配置。

<system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?"/>
    </authorization>
    <identity impersonate="true" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <pages>
    <namespaces>
      <add namespace="System.Web.Optimization" />
      <add namespace="Microsoft.AspNet.Identity" />
    </namespaces>
    <controls>
      <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
    </controls>
    </pages>
</system.web>

1 个答案:

答案 0 :(得分:0)

啊,这是多么愚蠢的错误。 在aspx文件中,之前我已经在文件开头添加了代码<%= User.Identity.Name%>。一旦删除,它就会按我的意愿工作。

谢谢。