SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'

I have created an application pool called "schoolPool" and assigned it to my web application. Identity for this pool has been set to LocalSystem. When I try to access my database from within the application, i.e. open a SQL connection, I get the following error all the time:

Login failed for user 'NT AUTHORITY\SYSTEM'


I tried to add NT AUTHORITY/SYSTEM to SSMS (SQL Server Management Studio) logins, but it was already a principal, showing the following error:
enter image description here


Allow NT AUTHORITY/SYSTEM to server Role as sysadmin. enter image description here

enter image description here


I tweaked the application settings a lot, changing the application pool's identity (in Windows 8.1's IIS) to LocalSystem, LocalService, NetworkService, and ApplicationPoolIdentity. However, all of them failed to solve the problem I had logging into my database.
Finally I set the pool identity on LocalSystem and thought why it might be preventing "NT AUTHRITY\SYSTEM" from opening a connection to my database. I opened up SQL Server Management Studio as "Administrator" and checked the Server Roles for NT AUTHORITY\SYSTEM under "logins" section. The default server role for this user was public by default. I also checked sysadmin and refreshed my web application form. This time it worked! Everything working perfectly now.


There is another fix. You should open Command Prompt (cmd) and write the following:

sqlcmd -S (server name)

select name from sys.server_principals where name = 'NT AUTHORITY\SYSTEM'

go

SP_ADDSRVROLEMEMBER 'NT AUTHORITY\SYSTEM','SYSADMIN'

go

The first line will give you an access to the sql server on you machine, the second will take the following result NT AUTHORITY\SYSTEM an the stored procedure addsrvrolemember will add sysadmin to it. Be careful, because you have to type the following code the way it is.


Rerun following query which will assign 'NT SERVICE\MSSQLSERVER' to sysadmin

EXEC master..sp_addsrvrolemember @loginame = N'NT SERVICE\MSSQLSERVER', @rolename = N'sysadmin'