简体   繁体   中英

How to connect to a database with trusted connection in ASP.NET MVC3?

I have an ASP.NET MVC3 application that uses Windows Authentication and the SqlRoleManager provider. I am trying to connect to a database with integrated security. The database team is requiring that each user be authorized to use the database using Windows authentication rather than a simple user name and password that can be used for the web application. I used the code below and found out that the principal is actually the app pool user. How do I attach the session to the current windows user? Is that even possible?

WindowsIdentity identity = WindowsIdentity.GetCurrent();
Response.Write(identity.Name);

Some things to try:

  • Turn on impersonation in the .config via identity .config node
  • Perform impersonation in the code around db access via WindowsImpersonationContext (never tried this - slow?)
  • Go through Context.User.Identity.Name (not sure if this fully gets you what you need).

Similar to this question .

Excerpt from MSDN Article :

The Context.User.Identity.Name property returns the authenticated user's identity. When the application runs in IIS 7, and impersonation is disabled, the Environment.UserName property returns the identity of the application pool that the Web application is running in. For example, if the Web site is running in the default application pool, the name DefaultAppPool is displayed. If the ASP.NET v4.0 application pool is used, ASP.NET v4.0 is displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM