简体   繁体   English

从ASP.NET页面获取Windows登录

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

Just like the title states. 就像标题所述。 Need the windows logon and domain info from within our asp.net page. 需要我们的asp.net页面中的Windows登录和域信息。

I tried 我试过了

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

but it returns the IIS App Pool not the user name 但它返回IIS应用程序池而不是用户名

Thanks 谢谢

Try HttpContext.User , accessible simply as User from the code behind. 尝试HttpContext.User ,可以从后面的代码中简单地作为User访问。 It returns both the domain and username, but should be easy enough to trim for your needs. 它返回域名和用户名,但应该很容易根据您的需要进行调整。 It's worked for me in the past. 它过去对我有用。 You can also use this to manage roles in your application, if you need to. 如果需要,您还可以使用它来管理应用程序中的角色。

EDIT 编辑
Below are the relevant portions of my web.config. 以下是我的web.config的相关部分。 I also used aspnet_regsql.exe to setup the tables needed for the role manager in my database. 我还使用aspnet_regsql.exe来设置数据库中角色管理器所需的表。 I could then use User.Identity.Name and User.Identity.IsInRole 然后我可以使用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>

This is my code in VB.net 这是我在VB.net中的代码

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

so C# must be in the lines of: (not tested) string strUser = System.Web.HttpContext.Current.User.Identity.Name; 所以C#必须在以下行中:(未经测试)字符串strUser = System.Web.HttpContext.Current.User.Identity.Name;

In the Web.Config file 在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