asp.net form authentication impersonate with SQL Serverhttp://forums.asp.net/t/1803172.aspx/1?asp+net+form+authentication+impersonate+with+SQL+ServerWed, 16 May 2012 02:14:09 -040018031724978327http://forums.asp.net/p/1803172/4978327.aspx/1?asp+net+form+authentication+impersonate+with+SQL+Serverasp.net form authentication impersonate with SQL Server <p>Dear Guys,</p> <p>I've a problem with asp.net forms authentication (with sqlserver membership provider) that the impersonation is not well for our AD Users, which is required for auditing database transactions</p> <p>&nbsp;</p> <p>when running the web applications sql server system_user becomes NT AUTHORITY\SYSTEM; by the way the connection string is like:</p> <p>Data Source=10.50.1.21;Initial Catalog=tmpIIMS;Integrated Security=True;Persist Security Info=False;Trusted_Connection=Yes;</p> <p><span color="#0000ff" size="2" style="color:#0000ff; font-size:small"><span color="#0000ff" size="2" style="color:#0000ff; font-size:small"></span></span>whic is authenticated by Integrated security with AD at IIS.</p> <p>&nbsp;</p> <p>so could any one help me to retreive at audit with a desired account for each AD Users</p> 2012-05-13T12:50:11-04:004978329http://forums.asp.net/p/1803172/4978329.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p></p> <blockquote><span class="icon-blockquote"></span> <h4>m_fuad67</h4> with a desired account for each AD Users</blockquote> <p></p> <p>put</p> <p>&lt;identity impersonate=true</p> <p>in web.config<br> http://msdn.microsoft.com/en-us/library/72wdk8cc%28v=vs.71%29.aspx</p> 2012-05-13T12:51:38-04:004978779http://forums.asp.net/p/1803172/4978779.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Hi m_fuad67,</p> <p>From your description, your ASP.NET web application is using forms authentication, however, you want to impersonate your web page's server-side code under specific user account (for accessing the backend SQL server database) ,correct?</p> <p>If so, I think you need to consider the following things first:</p> <p>* whether the user accounts used for the forms authentication are the same account set (same username/password) as used in your ASP.NET server's windows domain? For example, ASP.NET support using ActiveDirectory membership provider to validate username/password credentials against windows AD. In such case, the forms authentication username/password credential can be used to programmmatically impersonate and run ASP.NET code under the certain account:</p> <p>#How To: Use Impersonation and Delegation in ASP.NET 2.0 <br> <a href="http://msdn.microsoft.com/en-us/library/ff647404.aspx">http://msdn.microsoft.com/en-us/library/ff647404.aspx</a></p> <p>#Programmatically Impersonate a user in ASP.NET <a href="http://www.thescarms.com/dotnet/impersonate.aspx"> h<br> ttp://www.thescarms.com/dotnet/impersonate.aspx</a></p> <p>If not (the forms authentication accounts are just custom accounts that have no relationship to windows accounts in AD), then you can only make your ASP.NET web application to run under a specific account. You can either configure the&nbsp; IIS application pool identity to change the account under which your ASP.NET runs:</p> <p>#Application Pool Identities <br> <a href="http://learn.iis.net/page.aspx/624/application-pool-identities/">http://learn.iis.net/page.aspx/624/application-pool-identities/</a></p> <p>or you can still use impersonation, but specify a fixed account in web.config (also mentioend in the impersonation related articles above).</p> <p>In addition, here are some MSDN articles which can gives detailed explanation on how the ASP.NET forms and windows authentication work.</p> <p>#Explained: Forms Authentication in ASP.NET 2.0 <br> <a href="http://msdn.microsoft.com/en-us/library/ff647070.aspx">http://msdn.microsoft.com/en-us/library/ff647070.aspx</a></p> <p>#Explained: Windows Authentication in ASP.NET 2.0 <br> <a href="http://msdn.microsoft.com/en-us/library/ff647076.aspx">http://msdn.microsoft.com/en-us/library/ff647076.aspx</a></p> <p>&nbsp;</p> <p>&nbsp;</p> 2012-05-14T03:57:17-04:004979048http://forums.asp.net/p/1803172/4979048.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Hi Steven Cheng,</p> <p>First thanks for your cooperation, you are right our web application authentication is based on custom forms authentication (related to policy we cannot us AD or Windows auth,) but the usersnames will be the same of AD Accounts; the imersonate is set to true from beginning.</p> <p>&nbsp;</p> <p>I'll try advapi32.dll to try more.</p> 2012-05-14T06:30:10-04:004979257http://forums.asp.net/p/1803172/4979257.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Hi Steven,</p> <p>I did the impersonation using <strong>advapi32.dll </strong>but the sql server connection stril still taking the NT Service Username.</p> 2012-05-14T08:24:33-04:004979292http://forums.asp.net/p/1803172/4979292.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Thanks for the quick reply m_fuad67,</p> <p>After you impersonate via the security token (obtained by calling LogonUser API ), are you putting the SQL server accessing code right after you successfully impersonated the current context? Also, make sure you have turn off the original impersonation in web.config file. For testing, you can also try adding code to create a txt file on remote machine's share folder and check the created file's owner to see if the impersonated user account is used.</p> 2012-05-14T08:40:42-04:004979391http://forums.asp.net/p/1803172/4979391.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>I've check after the security token (by calling&nbsp;LogonUser API ) it seems it is not delegated for all web application only at login page, and the problem still with Application Pool my impersonation did not override the IIS Pool Auth.</p> <p>&nbsp;</p> <p>Login Code:</p> <p>&nbsp;</p> <pre class="prettyprint">IntPtr token = IntPtr.Zero; WindowsImpersonationContext impersonatedUser = null; try { bool result = LogonUser(LoginUser.UserName, &quot;ba&quot;, LoginUser.Password, LogonSessionType.Network, LogonProvider.Default, out token); if (result) { WindowsIdentity id = new WindowsIdentity(token); impersonatedUser = id.Impersonate(); // Log the new identity string secConn = &quot;Data Source=10.50.1.21; Initial Catalog=sec;Persist Security Info=False; Trusted_Connection=True; User ID=&quot; &#43; WindowsIdentity.GetCurrent().Name; string dataConn = &quot;Data Source=10.50.1.21; Initial Catalog=tmp; Persist Security Info=False; Trusted_Connection=True; User ID=&quot; &#43; WindowsIdentity.GetCurrent().Name; // Resource access here uses the impersonated identity if (Request.QueryString[&quot;ReturnUrl&quot;] != null) { FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, false); } else { FormsAuthentication.SetAuthCookie(LoginUser.UserName, false); Response.Redirect(&quot;~/default.aspx&quot;); } } else { Response.Write(&quot;&lt;/p&gt;LogonUser failed: &quot; &#43; Marshal.GetLastWin32Error().ToString()); } } catch { }</pre> <p></p> 2012-05-14T09:23:58-04:004979404http://forums.asp.net/p/1803172/4979404.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Yes, you' are right. Actually, the impersonation context just begins after the impersonate method get called (based on the logonUser API obtained token) and ends after the impersonation context is closed. And if you refer to the MSDN reference I mentioend, the impersonation scope is just within the&nbsp; try{....}finally{...} block. <br> <br> In other words, if you want to do programmtic impersonation, you need to do it at the place when you need to execute the certain privileged code. And after that , finish impersonation, you cannot impersonate one place and let the entire web application execute under that security context. Based on your requirement, you will need to keep the username/password credentials if you want to impersonate the user later (after perform login in the login page), but this is really not a good idea(saving clear text username/password). What do you think?</p> 2012-05-14T09:31:04-04:004979428http://forums.asp.net/p/1803172/4979428.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Yes, your are right, actually I can't do imersonation with saving at clear test username/pwd) especially because I'm the security consultant at this project; we have different solutions but the client wants authenticaton by mixing with ad and forms authentication, and this big effort just to take the identity whic is logged in for database audit triggers.</p> <p>&nbsp;</p> <p>My opinion is: I WILL CHANGE THE AUDIT CONCEPT COMPLETELY.</p> <p>I've the same question for you???</p> 2012-05-14T09:38:25-04:004982612http://forums.asp.net/p/1803172/4982612.aspx/1?Re+asp+net+form+authentication+impersonate+with+SQL+ServerRe: asp.net form authentication impersonate with SQL Server <p>Thanks for followup.</p> <p>Thus, you&nbsp;will need to change your database side audit to not use the login identity (from the ASP.NET ) since it will not reflect the actual forms authenticated user identiy. Will you create some store-procedure (which explicitly take identity info as parameter) for the db side auditing? Then, the ASP.NET side just call the SP to trigger the auditing (when data accessing occurs).</p> 2012-05-16T02:14:09-04:00