You restored a production backup to a new server (or to staging), and the application can't connect:
Login failed for user 'app_user'. (Microsoft SQL Server, Error: 18456)
-- or --
The server principal "app_user" is not able to access the database "Shop" under the current security context.
The user is right there under Database → Security → Users. Creating the login again doesn't help; now you get "User, group, or role already exists". The database user is orphaned.
Logins vs. users
SQL Server has two layers of identity:
- Logins are server-level (
master): they authenticate the connection. - Users are database-level: they're what permissions inside the database are granted to.
A user is linked to its login by the login's SID (security identifier), not by name. Windows logins have domain SIDs that are the same everywhere. SQL logins get a new, random SID on every server where they're created. Restore the database onto a server whose app_user login was created separately, and the SIDs don't match: the user points to a login that doesn't exist there.
Find orphaned users
USE Shop;
SELECT dp.name AS user_name, dp.type_desc, dp.sid
FROM sys.database_principals dp
LEFT JOIN sys.server_principals sp ON sp.sid = dp.sid
WHERE sp.sid IS NULL
AND dp.type IN ('S', 'U', 'G') -- SQL user, Windows user, Windows group
AND dp.authentication_type_desc IN ('INSTANCE', )
dp.principal_id ;
