System.Data.SqlClient.SqlException: Cannot open database "smg" requested by the login. The login failed.
System.Data.SqlClient.SqlException: Cannot open database "smg" requested by the login. The login failed.Login failed for user 'C02\ASPNET'
TThe resolution for this, is as follow:
- Create a Domain Account with priveleges similar to ASPNET or NETWORKSERVICE.
- Grant this Domain Account (DomainName/UserName) access in the SQL Server Database for the necessary databases.
- Use Impersonation in your web.config (setting identity impersonate="true") in the web.config of your application.Now, while enabling impersonation, you can either set the username and password in the web.config itself as follows:- However this defeats the process of security as you are again storing the password in the web.config file.The second method is to simply set identity impersonate to true and assign the username and password in the IIS.
To do that, do the following steps
- Type inetmgr from your command prompt and give enter.
- This would open the IIS Control Panel.
- Expand the appropriate nodes and select the Virtual Directory of your application.
- Right Click and select Properties.
- Switch to the Directory Security Tab.
- Under Anonymous access and authentication control click Edit
- Check the Enable Anonymous access in case you want people to access the application without logging in with Windows Logon Screen.
- Uncheck the Allow IIS to control password and enter the DomainName/UserName and Password in the respective boxes. Usually IIS uses IUSER_MACHINENAME credentials for Anonymous access.
- Uncheck if any other authentication mode is checked and then press Ok twice to exit.