Fix: “Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection" and "cannot open database requested by the login the login failed login failed for user"
Access a SQL Server Database from ASP.NET Web Application through Windows Authentication | Integrated Security=SSPI in Web.Config.
In general, we used to define the Connection String in Web.Config file to connect a database from a Web Application. That means SQL Server login credentials (User ID and Password) are maintained in Web.Config file as plain text which is not a secured approach. To overcome the security issue, we can implement standard Encrypt and Decrypt mechanism (System.Security.Cryptography) so that credentials can not be stolen or tampered very easily. However, the requirement taken into consideration here is completely different that is
“we need to enable the Windows authentication instead of SQL authentication so that credentials need not be stored in config file.”
Hence, enabling Windows Authentication could be an appropriate solution to this requirement. How to enable the Windows Authentication to connect SQL Server Database?
Windows Authentication for SQL Server database can be enabled with following changes,
1. ConnectionString need to be modified as follows, you could see that there is no User ID and Password specified in that, the keyword “integrated security=SSPI “takes care the authentication instead.
=========================================================
Advertisement: Choosing .NET Core Worker Services OR Windows Services?
=========================================================
Advertisement: Choosing .NET Core Worker Services OR Windows Services?
=========================================================
<add key="ConnectionString" value="data source=<SERVER NAME>; initial catalog=<DB NAME>; integrated security=SSPI; persist security info=False;"/>
2. Authentication mode should be mentioned as Windows only in the Web.Config file.
<authentication mode="Windows" />
3. Impersonation should be turned on under <system.web> section in Web.Config file
<identity impersonate="true"/>
Otherwise you may get the below error if not turned the Impersonation on.
If SQL Server exist in different machine,
“Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection”
If SQL Server exist in same machine,
“Cannot open database "DBNAME" requested by the login. The login failed. Login failed for user 'MachineName\ASPNET'.”
4. Finally, In case the SQL Server exists in different machine of the same domain then new login account to be added in SQL Server.
Go to SQL Server Management Studio, Login with Admin account, Under Security, Logins -> New Login then add new user with domain account details
Go to SQL Server Management Studio, Login with Admin account, Under Security, Logins -> New Login then add new user with domain account details
e.g. DOMAINNAME\USERNAME
After above changes, your web application can access the database without User Credentials, but based on Logged In user's privilege given already.
This seems a very simple right! Of course it is easy to enable the Windows authentication to connect the local SQL server or the SQL Server exists in different machine with in same domain. However, the real challenge will arise only when we enable the Windows Integrated Authentication for remote databases that exist outside the network domains where SQL server exist. How to access a remote SQL Server Database (not exist in same network domain)
Happy Learning!
Comments