I have an asp.net 4.5 web forms web app deployed on a remote IIS7+ server and I want to get the domain\username of the user to populate a database column.
My web.config has the following:
<system.web>
<authentication mode="Windows" />
<identity impersonate="true" /> <!-- I have tried with and without impersonate-->
<authorization>
<deny users="?"/>
</authorization>
...other stuff
</system.web>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
<modules>
<remove name="FormsAuthentication" />
</modules>
Here are some of the things I have tried:
String username = Membership.GetUser().UserName;
= HttpContext.Current.User.Identity.Name;
= Page.User.Identity;
= User.Identity.Name;
= Environment.Name; // Or something similar to this I don't remember exactly.
Other things to note:
Windows authentication is enabled on the server for my web app.
I keep getting the server name instead of the username.
Also, there is no logon for my web app. Users are restricted access to the server but if they can access the server then they automatically have access to my web app.
[UPDATE]
Interesting breakthrough, if I do an inline
<% Response.Write(HttpContext.Current.User.Identity.Name.ToString()); %>
then myDomain\username is written to my page. However, if I do the same code server side it returns the Server Name. Why would it return something different?
I have tried the below code but it still returns the Server name, I'm guessing its because the inline runs client-side and the controls run server side.
<asp:Label ID="LabelID" Text=" <% HttpContext.Current.User.Identity.Name.ToString(); %>" />
then in codeBehind
String curUser = LabelID.Text;