0

How to write the connection string to local SQL Server database using web.config?

Here is my connection string in web.config. But when I open local/demo, it said:

login failed for user ''.

Actually I'm using Windows authentication, so I thought it doesn't need user and password.

<connectionStrings>
    <add name="HRISContext" 
         connectionString="data source=.;initial catalog=BPSDEMO;Pooling=false;persist security info=True;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework" 
         providerName="System.Data.SqlClient"  />
</connectionStrings>
Dale K
  • 25,246
  • 15
  • 42
  • 71
riachan
  • 31
  • 1
  • 3
  • You need to add Integrated Security in connectionstring. `` For more information check https://www.connectionstrings.com/sqlconnection/ – Mohsin Mehmood Sep 01 '18 at 16:04
  • I already added the integrated security. Then the error: 'Login failed for user 'IIS APPPOOL\DefaultAppPool'. @MohsinMehmood – riachan Sep 01 '18 at 16:16
  • It indicates that your application is trying to connect to SQL database using Application pool identity. One way to fix is to add IIS AppPool\DefaultAppPool user to database. Checkout this SO answer for step by step instructions: https://stackoverflow.com/questions/7698286/login-failed-for-user-iis-apppool-asp-net-v4-0 – Mohsin Mehmood Sep 01 '18 at 16:32
  • How about this? ` ` The error message: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - @MohsinMehmood – riachan Sep 01 '18 at 16:35
  • This will only work if you have your database mdf file inside `App_Data` folder. Basically, this option allows you to deploy your database file along your project. – Mohsin Mehmood Sep 01 '18 at 16:54

1 Answers1

1

If you want to use Windows authentication, then yes, you must not include any user id and pwd in your connection string - just remove those completely. But you must include the Integrated Security=SSPI setting instead.

Try this:

<connectionStrings>
    <add name="HRISContext" 
         connectionString="data source=.;initial catalog=BPSDEMO;Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;persist security info=True;App=EntityFramework" 
         providerName="System.Data.SqlClient"  />
</connectionStrings>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459