简体   繁体   中英

Connecting to SQL Database from IIS

This is my connection string, which the web service (hosted in IIS, running under ApplicationPoolIdentity) uses to connect to database

<add name="AdventureWorksEntities"connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=TestMachine;Initial Catalog=AdventureWorks;
Integrated Security=True;Connection Timeout=60;multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />

When I try to access the site I am getting an exception: Login Failed for user MyDomain\\TestMachine$.

Why is IIS trying to use TestMachine to connect and not my user account (eg MyDomain\\TestUser) ? The reason I believe could be because its hosted under ApplicationPoolIdentity which uses machine creds.

How do I specify it to use the context of the user that made the request without having to run the application under custom identity (ie I want it to continue running under ApplicationPoolIdentity)?

If I set <identity impersonate = true/>' in my web.config file then I am able to access the site locally from IIS server machine and access to database works but then accessing the site remotely fails. My Webservice is using Windows Authentication. Any thoughts on this ?

You need to enable Windows Authentication and ASP.NET Impersonation in IIS

Then in your web.config

  <system.web>
    <authentication mode="Windows" />
    <identity impersonate="true" />
</system.web>

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