1

就像标题所说的那样。需要我们的 asp.net 页面中的 Windows 登录和域信息。

我试过了

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

但它返回 IIS 应用程序池而不是用户名

谢谢

4

2 回答 2

4

Try HttpContext.User,可以User从后面的代码中轻松访问。它返回域和用户名,但应该很容易根据您的需要进行修剪。过去它对我有用。如果需要,您还可以使用它来管理应用程序中的角色。

编辑
下面是我的 web.config 的相关部分。我还使用aspnet_regsql.exe在我的数据库中设置角色管理器所需的表。然后我可以使用User.Identity.NameUser.Identity.IsInRole

<connectionStrings>
    <clear/>
    <add name="SqlRoleManagerConnection"
         connectionString="myConnectionString">
    </add>
  </connectionStrings>

  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <deny users="?"/>
    </authorization>
    <roleManager enabled="true" defaultProvider="SqlRoleManager">
      <providers>
        <clear/>
        <add name="SqlRoleManager"
             type="System.Web.Security.SqlRoleProvider"
             connectionStringName="SqlRoleManagerConnection"
             applicationName="myAppName" />
      </providers>
    </roleManager>
  </system.web>
于 2013-11-13T21:10:19.530 回答
0

这是我在 VB.net 中的代码

var strUser = System.Web.HttpContext.Current.User.Identity.Name

所以 C# 必须在以下行中:(未测试) string strUser = System.Web.HttpContext.Current.User.Identity.Name;

在 Web.Config 文件中

<configuration>
    <system.web>
        <authentication mode="Windows"/>
        <identity impersonate="true" />
    </system.web>
</configuration>
于 2013-11-13T21:37:26.007 回答