<p>We have an ASP.NET application that manages it's own User, Roles and Permission database and we have recently added a field to the User table to hold the Windows domain account.  </p>

<p>I would like to make it so that the user doesn't have to  <strong>physically</strong> log in to our application, but rather would be automatically logged in based on the currently logged in Windows domain account DOMAIN\username.  We want to authenticate the Windows domain account against our own User table.  </p>

<p>This is a piece of cake to do in Windows Forms, is it possible to do this in Web Forms?</p>

<p>I don't want the user to be prompted with a Windows challenge screen,  I want our system to handle the log in.</p>

<p><strong>Clarification</strong>: We are using our own custom Principal object.</p>

<p><strong>Clarification</strong>: Not sure if it makes a difference or not, but we are using IIS7.</p>

[{'answer_id': 54065, 'author': 'Biri', 'author_id': 968, 'author_profile': 'https://Stackoverflow.com/users/968', 'pm_score': 1, 'selected': False, 'text': '<pre><code>using System.Security.Principal;\n...\nWindowsPrincipal wp = (WindowsPrincipal)HttpContext.Current.User;\n</code></pre>\n\n<p>to get the current domain user. Of course you have to make sure that the IIS is set up to handle Windows Authentication.</p>\n'}, {'answer_id': 54081, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': "<p>This might be helpful:    </p>\n\n<pre><code>WindowsIdentity myIdentity = WindowsIdentity.GetCurrent();\n\nWindowsPrincipal myPrincipal = new WindowsPrincipal(myIdentity);\n\nstring name = myPrincipal.Identity.Name;\nstring authType = myPrincipal.Identity.AuthenticationType;\nstring isAuth = myPrincipal.Identity.IsAuthenticated.ToString();\n\nstring identName = myIdentity.Name;\nstring identType = myIdentity.AuthenticationType;\nstring identIsAuth = myIdentity.IsAuthenticated.ToString();\nstring iSAnon = myIdentity.IsAnonymous.ToString();\nstring isG = myIdentity.IsGuest.ToString();\nstring isSys = myIdentity.IsSystem.ToString();\nstring token = myIdentity.Token.ToString();\n</code></pre>\n\n<p>Disclaimer: I got this from a technet article, but I can't find the link.</p>\n"}, {'answer_id': 54098, 'author': 'pete blair', 'author_id': 5119, 'author_profile': 'https://Stackoverflow.com/users/5119', 'pm_score': 2, 'selected': True, 'text': '<p>I did pretty much exactly what you want to do a few years ago. Im trying to find some code for it, though it was at a previous job so that code is at home.</p>\n\n<p>I do remember though i used <a href="http://msdn.microsoft.com/en-us/library/ms998360.aspx" rel="nofollow noreferrer">this article</a> as my starting point. You set up the LDAP provider so you can actually run a check of the user vs the LDAP. One thing to make sure of if you try the LDAP approach. In the setting file where you set up the LDAP make sure LDAP is all caps, if it is not it will not resolve.</p>\n'}, {'answer_id': 54160, 'author': 'Omer van Kloeten', 'author_id': 4979, 'author_profile': 'https://Stackoverflow.com/users/4979', 'pm_score': 0, 'selected': False, 'text': '<p>You can use <code>System.Threading.Thread.CurrentPrincipal</code>.</p>\n'}, {'answer_id': 57311, 'author': 'John Sheehan', 'author_id': 1786, 'author_profile': 'https://Stackoverflow.com/users/1786', 'pm_score': 0, 'selected': False, 'text': '<p>Request.ServerVariables["REMOTE_USER"]</p>\n\n<p>This is unverified for your setup, but I recall using this awhile back.</p>\n'}, {'answer_id': 108799, 'author': 'blowdart', 'author_id': 2525, 'author_profile': 'https://Stackoverflow.com/users/2525', 'pm_score': 2, 'selected': False, 'text': '<p>Integration of this sort is at the server level, it\'s IIS that decides that the user is not logged in; and it\'s IIS that sends back the authentication prompt to the user, to which the browser reacts.</p>\n\n<p>As you want to use the domain login there is only one way to do this; <a href="http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/1aa70bfa-add5-4f61-9c7b-a095c1bd4306.mspx?mfr=true" rel="nofollow noreferrer">integrated windows authentication</a>. This will only work if the IIS server is also part of the domain and the users are accessing the machine directly, not through a proxy, and from machines which are also part of the domain (with the users suitably logged in).</p>\n\n<p>However your custom principal object may create fun and games; authentication of this type will be a WindowsPrincipal and a WindowsIdentity; which you can access via the User object (see <a href="http://msdn.microsoft.com/en-us/library/ms998358.aspx" rel="nofollow noreferrer">How To: Use Windows Authentication in ASP.NET 2.0</a>)</p>\n\n<p>I assume you want a custom principal because of your custom roles? I doubt you can get the two to play nicely; you could create a custom <a href="http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.aspx" rel="nofollow noreferrer">role provider</a> which looks at your data store or look at you could look at <a href="http://msdn.microsoft.com/en-us/library/ms998358.aspx" rel="nofollow noreferrer">ADAM</a>, an extension to AD which provides roles on a per program basis and comes with nice management tools.</p>\n'}, {'answer_id': 1106196, 'author': 'CodingBytes', 'author_id': 1303, 'author_profile': 'https://Stackoverflow.com/users/1303', 'pm_score': 0, 'selected': False, 'text': '<p>Try Request.ServerVariables("LOGON_USER").</p>\n\n<p>If the directory security options are set so that this directory does not allow anonymous users, when the surfer hits this page they will be prompted with the standard modal dialog asking for username and password.  Request.ServerVariables("LOGON_USER") will return that user.</p>\n\n<p>However, this will probably not work for you because you are using your own custom security objects.  If you can figure out how to get around that logon box, or pass in NT credentials to the site before it askes for them, then you would be all set.</p>\n'}, {'answer_id': 1109738, 'author': 'CodingBytes', 'author_id': 1303, 'author_profile': 'https://Stackoverflow.com/users/1303', 'pm_score': 0, 'selected': False, 'text': '<p>Have you thought about impersonation?  You could store the user\'s NT logon credentials in your custom security object, and then just impseronate the user via code when appropriate.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa292118(VS.71).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa292118(VS.71).aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/54050', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1768/']