[英]How do I get current user logged in to windows?
I am an active directory user and I am simply trying to print the name of the current user out in a Respnse.Write()
method. 我是活动目录用户,我只是想在
Respnse.Write()
方法中打印当前用户的名称。 From what I read from several other questions posted here I need to use 从我从这里发布的其他几个问题中读到的内容中我需要使用
using System.Security.Principal;
string username = WindowsIdentity.GetCurrent().Name
However, when I try to write the username to the screen I get 但是,当我尝试将用户名写入屏幕时,我得到
NT AUTHORITY\\NETWORK SERVICE NT AUTHORITY \\ NETWORK服务
instead of 代替
domain\\12345678 域\\ 12345678
Here is the code I am using to write to the screen: 这是我用来写到屏幕上的代码:
Response.Write(WindowsIdentity.GetCurrent().Name);
and I have identity impersonate set to true in my web.config. 并且我在web.config中将模拟身份设置为true。 What do I do next?
接下来我该怎么办?
Edited to show suggested answers 编辑以显示建议的答案
my pageload 我的页面加载
protected void Page_Load(object sender, EventArgs e)
{
string userName = User.Identity.Name;
Response.Write(userName);
//currently returning null
}
In your web.config you need authentication mode
switched on to Windows
and you need to disabled anonymous users 在web.config中,您需要将
authentication mode
打开到Windows
并且需要禁用匿名用户
<system.web>
<authentication mode="Windows" />
<anonymousIdentification enabled="false" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
The reason your approach isn't working is because User.Identity
isn't physically referencing your Active Directory Membership . 您的方法不起作用的原因是
User.Identity
并未物理引用您的Active Directory成员身份 。 For all intensive purposes, is trying to grab your active user through Internet Information Systems (IIS) which it can't do in the current state. 出于所有密集目的,我们正在尝试通过Internet信息系统(IIS)来吸引您的活动用户,而这在当前状态下是不行的。 Since your utilizing Web-Forms, the easiest approach would be:
由于您使用的是Web表单,最简单的方法是:
<asp:LoginView>
: The following template will allow you to specify visible data for an anonymous user, logged in user, or logged out user. <asp:LoginView>
:以下模板将允许您为匿名用户,登录用户或注销用户指定可见数据。 Which will help manage your membership system accordingly. 这将有助于相应地管理您的会员系统。
Membership Not Needed - Membership isn't regulated, but would like to display or access whom is logged in for certain instances. 不需要成员资格-成员资格不受限制,但想显示或访问在某些情况下登录的人。
To implement: 实现:
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://..." />
</connectionStrings>
That will be your connectionString
to your directory. 那将是您到目录的
connectionString
。 Now to ensure the application authenticates correctly: 现在确保应用程序正确认证:
<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>
Now you'll be able to properly run User.Identity
. 现在,您将能够正确运行
User.Identity
。
Hopefully that helps. 希望有帮助。
string yourVariable = User.Identity.Name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.