4

I'm trying to use HttpContext.Current.User.Identity.Name to get the user's Windows login details from the internal network, however it's empty.

I've tried changing the authentication mode in Web.Config with no joy:

  <system.web>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

I've also heard that in the project properties you should enable Windows authentication and disable anonymous authentication like so:

enter image description here

But then I get a re-direct loop, with the message "This webpage has a redirect loop" in Chrome.

I've also checked that Windows Authentication is installed on my machine:

enter image description here

Any ideas on how to fix this please?

Many thanks

Nick
  • 3,745
  • 20
  • 56
  • 75
  • 1
    Are you using this method while user login: FormsAuthentication.SetAuthCookie("user name",true); – Vikram Aug 01 '14 at 12:39
  • Thank you, I've just tried adding that into my Home Controller but unfortunately I still get the same error – Nick Aug 01 '14 at 12:41
  • please try with default settings(as you have modified them) – Vikram Aug 01 '14 at 12:43
  • 1
    This is working for me: @if (Request.IsAuthenticated) { @Html.Encode(User.Identity.Name) @Html.ActionLink("Sign Out", "Logout", "Home") } – Vikram Aug 01 '14 at 12:46
  • Thanks, I've just tried @Html.Encode(User.Identity.Name) and it's null again :( – Nick Aug 01 '14 at 12:49
  • I think we have some problem with 'Windows authentication'. – Vikram Aug 01 '14 at 12:51
  • Yes, I've created a system in WebForms before and it worked fine... it's very odd that it isn't working now – Nick Aug 01 '14 at 12:55
  • 1
    If it's on your local workstation, you might want to try setting the – Lareau Aug 01 '14 at 13:04
  • Thank you, unfortunately I now get an 'An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.' error. – Nick Aug 01 '14 at 13:18

2 Answers2

7

When I have:

<authentication mode="Windows"/>
<identity impersonate="true/>

in web.config I get the current user with:

string currUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
Martin Smellworse
  • 1,702
  • 3
  • 28
  • 46
  • Thank you very much for the reply, however I get the following error: _An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode. Most likely causes: system.web/identity@impersonate is set to true._ – Nick Aug 01 '14 at 14:17
  • Further to the above, I got rid of the error: http://stackoverflow.com/questions/4209999/an-asp-net-setting-has-been-detected-that-does-not-apply-in-integrated-managed-p However unfortunately the value `System.Security.Principal.WindowsIdentity.GetCurrent().Name` of is still null :( – Nick Aug 01 '14 at 14:20
0

Changing my project web server settings to Local IIS instead of IIS Express resolved the issue for me, though I'm not sure why this is the case if anyone has additional information about this.

  • Right click the project
  • Click Properties
  • Go to the Web tab
  • In the Servers section, select Local IIS from the dropdown
whotyjones
  • 133
  • 9