1

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
}
Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • try turning on windows authentication in IIS and looking in Request.ServerVariables["REMOTE_USER"]; for the username – WraithNath Jan 26 '15 at 17:11
  • possible duplicate of [How to get at the current users windows identity](http://stackoverflow.com/questions/5230441/how-to-get-at-the-current-users-windows-identity) – Fred Jan 26 '15 at 17:15
  • @WraithNath currently, authentication mode is set to windows – Skullomania Jan 26 '15 at 18:50

3 Answers3

3

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>
Hugo Yates
  • 2,081
  • 2
  • 26
  • 24
  • I did this and it still get `NT AUTHORITY\NETWORK SERVICE` as a print out. – Skullomania Jan 26 '15 at 18:52
  • @Skullomania Do what's said in this answer, but also use `User.Identity.Name` to get the username in your code. – mason Jan 26 '15 at 19:30
  • i kept the changes you requested in the webconfig and in the page_load I used `string userName = User.Identity.Name;Response.Write(userName);` and userName returns a null value – Skullomania Jan 26 '15 at 19:40
  • Are you doing this in IE? You may need check `Enable Integrated Windows Authentication` in the `Options` > `Advanced` tab. Also don't use `Response.Write`, create a label or literal and write to that. – Hugo Yates Jan 26 '15 at 19:53
1

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.

Greg
  • 11,302
  • 2
  • 48
  • 79
0
string yourVariable = User.Identity.Name;
Ilnetd
  • 89
  • 8