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. 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
instead of
domain\\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. 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
<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 . 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. Since your utilizing Web-Forms, the easiest approach would be:
<asp:LoginView>
: The following template will allow you to specify visible data for an anonymous user, logged in user, or logged out user. 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. 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
.
Hopefully that helps.
string yourVariable = User.Identity.Name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.