Users Indistinguishable in Hosted ASP.Net Application - c#

I am working on a hosted ASP.Net application, with Windows Authentication, and I need to get the username for various personalisation tasks. The directory on the host machine is set to have Windows Authentication and the web.config file also has:-
<authentication mode="Windows">
</authentication>
in it. I have tried various methods within the application to get the user name with the following results:-
Source Result
System.Web.HttpContext.Current.Request.LogonUserIdentity.Name 'EZ\urlname_a'
System.Environment.UserName 'NETWORK SERVICE'
System.Web.HttpContext.Current.User.Identity.Name ''
this.Request.ServerVariables["LOGON_USER"] ''
this.Request.ServerVariables["AUTH_USER"] ''
this.User.Identity.Name ''
thanks to the answers here, here, and here, among others. None of these distinguish between the various usernames that were entered.
Is there anything else I can try in code to get the name (or some proxy for the name)? Is there anything I can check for in web.config? Or if I need to take the matter up with the host, is there any intelligent question I can ask?
Edit I have set the application directory and .Net version using the host's IIS Dialogue.

You need to set IIS to use Windows Authentication only, then use Page.User.Identity.Name or similar to fetch the windows user name.
From the Authentication pane, set the Windows Authentication to Enabled and Disable anonymous authentication.

Related

C# asp.net - get windows user login account on deploy server

I am using C# asp.net to get windows login account (domain\username) for login my web automatically. I can get the account correctly when run the statement "System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()" locally on visual studio. However, after deploying the web page to deploy server, it returns "NT AUTHORITY\IUSR".
Most of the solution I found on internet is to use Windows Authentication instead of Anonymous Authentication. I have tried this solution and a user login window is prompted when run the web. It needs to enter the username and password for the deployed server. However, my windows user login account cannot login to the deploy server. Therefore I cannot run the web on my computer.
I would like to know is it possible to get the window user login account if my window account cannot login to the deploy server?
Please try to enable two Authentication methods. First you have to enable Windows Authentication like you already mentioned to pass your credentails automatically to the server (of course only in Intranet).
Additionally you have to activate ASP.NET Impersonation to allow the server to use the passed credentials.
The Help tells you why:
Use ASP.NET Impersonation authentication when you want to run your
ASP.NET application under a security context different from the
default.
In your web.config you have to add the following nodes to your system.web node to tell your application to pass your credentials to the server:
<identity impersonate="true"/>
<authentication mode="Windows" />

ASP.Net MVC4 User.Identity.Name getting empty even though set authentication mode as 'Windows'

I am working on ASP.Net MVC-4 application. I have to implement windows authentication.
I have set authentication mode as 'Windows' in web.config file as shown below.
<system.web>
<authentication mode="Windows" />
</system.web>
In controller I try to get username as below.
string userName = User.Identity.Name;
but every time I am getting empty value.
Please let me know for any suggestions.
Thanks in advance.
Windows authentication is performed by IIS to establish our managed code User.Identity. Therefore, you need to enable Windows Authentication in your IIS, and in order to force the user to authenticate before being able to access our application, you need to disable Anonymous Authentication
With the given information, it looks like you've configured your project correctly but haven't actually authenticated the user yet.
First some background. There is a simplified tutorial on asp.net where, in between the lines, the following statement is mentioned:
By default, the ASP.NET Development Web Server executes all pages in the context of the current Windows account (whatever account you used to log into Windows).
This means that when you run your project with F5, it executes everything under your currently logged in user account. However, it isn't yet authenticated for the application and therefor your User.Identity.* is not set yet.
In order to verify if this is the case, you should add the [Authorize] attribute on the first controller (or Action) that is called in you ASP.NET MVC project. Most likely you'll be confronted with a "HTTP Error 401.0 - Unauthorized" exception. In this case, you should enable your webserver to authenticate first. The above mentioned tutorial will help you with this.

asp net: File Exists returns false for UNC path but doesn't when accessing via localhost

I have a web site that requests users access via IIS 6 using Integrated Windows authentication then there is a section of the webpage that tests whether a file is present using:
File.Exists(sourcePath)
Where the sourcePath is a UNC that the user should be able to access. Currently my issue that while the user can access this UNC path outside of the web site I get a false result.
Now the strange thing is that the test is successfully if I use
http://localhost/Site
on the server hosting the website but fails if I use
http://[machine name]/Site
This feels like a permissions issue but struggling to nail down the cause of this.
Extra details:
UNC path is on a NAS server
web config has identity impersonate="true"
Tried adding user configuration for a user with access to the UNC in the app pool. No change in issue
You'll want to make sure that you are explicitly setting the user name and password when impersonating an identity for UNC paths, like so:
<identity impersonate="true" userName="accountname" password="password" />
This sounds like an issue with Kerberos. I'd guess that there isn't an SPN configured correctly for the identity the app pool is running under.

Get Windows user credentials with windows authorization disabled in IIS

Is it possible to grab a users windows credentials (i.e. username) without having windows authentication enabled in IIS? With windows auth disabled the code below returns either NT AUTHORITY or IIS APPPOOL\ASP.NET v4.0 depending on if impersonation and anonymous authentication are enabled or not.
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
I am converting an asp.net web application that is using forms authentication to custom authentication. Basically if a user is connecting from outside the network I force a credential check whereas if they are connecting from within the network I would like to be able to just grab their windows username. The internal portion works when I turn on windows authentication but I get the popup login box when testing outside of the network. I either need to disable windows authentication for non local connections or figure out how to get the windows username with windows authentication disabled. Any suggestions? (The other alternative i thought of was splitting the application in two and having separate authentication modes for each but I'd like to avoid this).
-I have also tried:
System.Web.HttpContext.Current.User.Identity.ToString();
System.Threading.Thread.CurrentPrincipal.ToString();
something like this is actually (kind of) possible using Active Directory Federated Services. In the event of a windows user from inside the network they can be configured to use their account details. For external users, they can be redirected to a page that will require them to log in.
However. This is very heavy-duty and an extreme pain in the..neck to implement and is really only applicable to enterprise solutions that have the resourses to use this kind of solution. Otherwise, I'd say go with the 2 site approach.
Simple answer is no.
Best solution is to create 2 sites. One for internal users that user windows authentication, one for external user that user forms authentication but authenticate against AD. You can make users always go to external site by default and then redirect based on their IP. That is redirect intranet users to internal user.

Windows Authentication Doesn't automatically grab user credentials

I have an aspx site that I'm working on for our company's intranet. I recently added some secure pages that require the user to be a member of particular groups in an Active Directory in order to view. We are using Windows Authentication for the site(I have windows authentication in the .config). Windows Authentication is enabled in the IIS, and Anonymous Authentication is disabled. I've also enabled NTLM Authentication in the projects properties.
As far as I can tell, the security stuff is working as expected. Only users with the proper credentials can access the secure pages (I'm securing them with the [Authenticate Roles = "bla"] check on the controller action).
The problem I'm having is not really a problem, but more of an annoyance. Whenever the user logs in to the site, they are prompted with a login dialog. I don't want this. I want the site to grab their credentials from the windows login and use that to determine their access rights. I was under the impression that Windows Authentication handled this on its own, but it appears I was wrong.
Basically, how can I get rid of the login prompt and have Windows Authentication handle all of that same functionality automatically?
Is there some server setting I might need to change? Could it be something in my code?
I want it to work with at least IE, Firefox, and Chrome, if that is at all possible.
***Update 7/23/2012
Thanks everyone for the suggestions, unfortunately I still haven't gotten this to work properly. Some things I've noticed that may help provide some more details
I'm fairly certain the intranet site is on our list of "trusted" sites (our network admin says it is).
I'm using NTLM authentication and NTLM authentication only. If I remove NTLM authentication and enable Negotiate: Kerberos authentication, I just get a 401 - Unauthorized error. I can fix this by disabling Kernel mode authentication, but then I still get the credentials prompt(which I don't want).
If I check "Enable Integrated Windows Authentication*" in IE > Internet Options > Advanced > Security, it will prompt me for credentials, but entering my credentials no longer works. It will ask me three times and then take me to the 401 error page.
IE9 asks me only for my password and pulls my username(good). Chrome and Firefox prompt me for username and password.
Look in Internet Explorer / Tools / Options / Advanced.
There is a checkbox "Enable Integrated Windows Authentication" under "Security".
Is this checked?
It's checked by default, and can be set by admins using a GPO:
http://www.windowsecurity.com/articles/configuring-advanced-ie-settings-using-group-policy.html
There is a setting within IE that allows this automatic pass through to happen. Your system administrator could create a group policy and push this to all users.
I've also been successful in configuring Firefox to function in the same matter but that would involve modifying individual users FF configurations.
This line should be in your web.config within the <system.web> element.
<authentication mode="Windows" />
Just thought I'd provide an update as to what actually solved the problem.
I tried all of the great suggestions you guys provided for internet explorer settings, but in the end, it turned out to be a server setting.
Flipping the order of Providers for the site to NTLM, Negotiate in that order solved the problem.

Categories