我是一名活跃的目录用户,我只是想用Respnse.Write()
方法打印出当前用户的名字。从我从这里发布的其他几个问题中读到的,我需要使用
using System.Security.Principal;
string username = WindowsIdentity.GetCurrent().Name
然而,当我尝试将用户名写入屏幕时,我得到了
NT AUTHORITY \ NETWORK SERVICE
而不是
域\ 12345678
以下是我用来写入屏幕的代码:
Response.Write(WindowsIdentity.GetCurrent().Name);
我在web.config中将identity impersonate设置为true。我接下来该怎么办?
编辑以显示建议的答案
我的页面加载
protected void Page_Load(object sender, EventArgs e)
{
string userName = User.Identity.Name;
Response.Write(userName);
//currently returning null
}
答案 0 :(得分:3)
在您的web.config中,您需要authentication mode
已启用Windows
,您需要禁用匿名用户
<system.web>
<authentication mode="Windows" />
<anonymousIdentification enabled="false" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
答案 1 :(得分:1)
您的方法无效的原因是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
。
希望这有帮助。
答案 2 :(得分:0)
string yourVariable = User.Identity.Name;