Login fails for Users Created for a login

Mike Toubasi 0 Reputation points
2023-08-07T19:40:48.63+00:00
	-- in master
	IF not exists(SELECT * FROM sys.sql_logins  WHERE name = 'lgnApp')
	BEGIN
		CREATE LOGIN lgnApp WITH PASSWORD = 'Appuser_123';
	END

	-- For each database
	IF not exists(SELECT * FROM sys.database_principals  WHERE name = 'AppUser')
	BEGIN
		CREATE USER AppUser for login lgnApp WITH DEFAULT_SCHEMA = dbo;
	END

SQL Server | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.7K Reputation points MVP Volunteer Moderator
    2023-08-07T20:51:47.11+00:00

    Look in the SQL Server errorlog, where you will get details on why the login failed.

    0 comments No comments

  2. RahulRandive 10,486 Reputation points Volunteer Moderator
    2023-08-07T22:28:56.84+00:00

    Hi @Mike Toubasi

    How about following below steps- (assuming you are Creating SQL Login and SQL User on your Azure SQL DB)
    /1: Create SQL Login on master database (connect with admin account to master database)/

    CREATE LOGIN MaryLogin WITH PASSWORD = '<strong_password>';

    /2: Create SQL user on the master database (this is necessary for login attempt to the <default> database, as with Azure SQL you cannot set the DEFAULT_DATABASE property of the login so it always will be [master] database.)/

    CREATE USER MaryUser FROM LOGIN MaryLogin;

    /3: Create SQL User on the user database (connect with admin account to user database)/

    CREATE USER MaryUser FROM LOGIN MaryLogin;

    /4. Grant permissions to the user by assign him to a database role/

    ALTER ROLE db_datareader ADD MEMBER MaryUser;

    Thank you!

    0 comments No comments

  3. ZoeHui-MSFT 41,491 Reputation points
    2023-08-08T02:27:55.32+00:00

    Hi @Mike Toubasi,

    Check with below code.

    IF not exists(SELECT * FROM sys.sql_logins  WHERE name = 'lgnApp')
    	BEGIN
    		CREATE LOGIN lgnApp WITH PASSWORD = 'Appuser_123';
    	END
    
    CREATE USER AppUser for login [lgnApp];
    GRANT CONTROL ON schema::[dbo] TO [AppUser];
    
    
    

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.