I am using a contained database, after creating the database I attempted to create a user but I got the error:
You can only create a user with a password in a contained database
My code is:
sp_configure 'show advanced options',1 GO RECONFIGURE WITH OVERRIDE GO sp_configure 'contained database authentication', 1 GO RECONFIGURE WITH OVERRIDE GO
CREATE DATABASE [MyDb] CONTAINMENT = PARTIAL ON PRIMARY ( NAME = N'My', FILENAME = N'C:\My.mdf') LOG ON ( NAME = N'My_log', FILENAME =N'C:\My_log.ldf')
CREATE USER MyUser WITH PASSWORD = 'pass@123'; GO
Contained database users are users that are configured directly at the database level, and don't require an associated login in the master database. The benefit of this approach is that it makes your database more portable - and it also simplifies database deployments a little.
You do not need to run the ALTER DATABASE ... SET CONTAINMENT command on Azure SQL DBs to accept contained users - it is already enabled by default. You simply need to create the user with just a login and password. A simple example of a contained user with password: CREATE USER yourUser WITH PASSWORD = 'yourPassword';
A contained database includes all database settings and metadata required to define the database and has no configuration dependencies on the instance of the Database Engine where the database is installed. Users can connect to the database without authenticating a login at the Database Engine level.
Expand the database in which to create the new database user. Right-click the Security folder, point to New, and select User.... In the Database User - New dialog box, on the General page, select one of the following user types from the User type list: SQL user with login.
Create the login and the user separately:
CREATE LOGIN MyUser WITH PASSWORD = 'pass@123'; CREATE USER MyUser FOR LOGIN MyUser;
The names can be the same but of course they can also be different.
In SQL Server 2017, I found that my initial setup did not configure the "contained databases" feature to help this along.
so, at server level you can check the server properties in the UI or run:
EXEC sp_configure 'CONTAINED DATABASE AUTHENTICATION'
if the running value isn't 1, then:
EXEC sp_configure 'CONTAINED DATABASE AUTHENTICATION', 1 GO RECONFIGURE GO
At the database level, it also might not have the "contained database" feature fully enabled. The option sits in the Database Properties panel on the Options section, the fourth dropdown at the top...
Containment type == None or Partial
You can set it via SQL too. eg:
USE [master] GO ALTER DATABASE [MyDb] SET CONTAINMENT = PARTIAL GO
thereafter, you can create the contained user as suggested by @aleksandr
USE [MyDb] GO CREATE USER MyUser WITH PASSWORD = 'pass@123'; GO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With