繁体   English   中英

从ASP.NET页面获取Windows登录

[英]Get Windows Logon From Within ASP.NET Page

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

我试过了

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

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

谢谢

尝试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>

这是我在VB.net中的代码

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

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

在Web.Config文件中

<configuration>
    <system.web>
        <authentication mode="Windows"/>
        <identity impersonate="true" />
    </system.web>
</configuration>

暂无
暂无

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

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