繁体   English   中英

如何让当前用户登录Windows?

[英]How do I get current user logged in to windows?

我是活动目录用户,我只是想在Respnse.Write()方法中打印当前用户的名称。 从我从这里发布的其他几个问题中读到的内容中我需要使用

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

但是,当我尝试将用户名写入屏幕时,我得到

NT AUTHORITY \\ NETWORK服务

代替

域\\ 12345678

这是我用来写到屏幕上的代码:

Response.Write(WindowsIdentity.GetCurrent().Name);

并且我在web.config中将模拟身份设置为true。 接下来我该怎么办?

编辑以显示建议的答案

我的页面加载

protected void Page_Load(object sender, EventArgs e)
{
    string userName = User.Identity.Name; 
    Response.Write(userName);
    //currently returning null
}

在web.config中,您需要将authentication mode打开到Windows并且需要禁用匿名用户

<system.web>
  <authentication mode="Windows" />
    <anonymousIdentification enabled="false" />
    <authorization>
      <deny users="?" />
    </authorization>
</system.web>

您的方法不起作用的原因是User.Identity并未物理引用您的Active Directory成员身份 出于所有密集目的,我们正在尝试通过Internet信息系统(IIS)来吸引您的活动用户,而这在当前状态下是不行的。 由于您使用的是Web表单,最简单的方法是:

  • <asp:LoginView> :以下模板将允许您为匿名用户,登录用户或注销用户指定可见数据。 这将有助于相应地管理您的会员系统。

  • 不需要成员资格-成员资格不受限制,但想显示或访问在某些情况下登录的人。

实现:

<connectionStrings>
     <add name="ADConnectionString" connectionString="LDAP://..." />
</connectionStrings>

那将是您到目录的connectionString 现在确保应用程序正确认证:

<authentication mode="Windows">
  <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
  <providers>
    <clear />
    <add name="MyADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" />
  </providers>
</membership>

现在,您将能够正确运行User.Identity

希望有帮助。

string yourVariable = User.Identity.Name;

暂无
暂无

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

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