In every database engine it is important to create logins that enforce security around your database and that can be maintained.
Additionally if you are working for a client you may wish to transfer this database at some point in the future to the client.
In SQL Azure users can be created against the master database in the instance and the role can then be transferred to individual databases.
Fine but there may be circumstances where you want to isolate roles to individual databases so that when they are moved the roles move with them and are not left in the master database.
The following sets out an example of how to set up a Contained database in SQL Azure along with something extra you have to think about when re-importing to an SQL Server instance.
Using your sysadmin account connect to the database you wish to add a user to and run;
CREATE USER rocketengineapplication WITH PASSWORD = 'bluedanube'; ALTER ROLE db_owner ADD MEMBER rocketengineapplication;
Now developers could use this password and username to login to the database and do most of what is required without having any privileges to the SQL Server and if you ever transfer the database the role will pass with the database.
Here is a link to built in database roles
SQL Database Roles
and here is a useful query that can be run to identify the users and roles that a particular database has. This allows you to check what users are on a database and what are there roles.
SELECT u.name AS UserName, u.type_desc AS UserType, r.name AS RoleName FROM sys.database_principals AS u LEFT JOIN sys.database_role_members AS rm ON rm.member_principal_id = u.principal_id LEFT JOIN sys.database_principals AS r ON r.principal_id = rm.role_principal_id WHERE u.type NOT IN('R', 'G') ORDER BY UserName , RoleName;
Note that when deploying or importing data tier applications to for instance SQL Express versions by default contained database authentication is deactivated and must be activated.
To do this connect to the local sql express instance and highlight the Databases on the left hand side then run the following code
sp_configure 'contained database authentication', 1; GO RECONFIGURE; GO
DACPAC (structure only) and BACPAC (data and structure) import should now be possible locally!
This will specifically assist with the following error message which I was getting when I tried to import the database back into a local machine.
TITLE: Microsoft SQL Server Management Studio ------------------------------ Could not deploy package. Error SQL72014: .Net SqlClient Data Provider: Msg 12824, Level 16, State 1, Line 5 The sp_configure value 'contained database authentication' must be set to 1 in order to alter a contained database. You may need to use RECONFIGURE to set the value_in_use. Error SQL72045: Script execution error. The executed script: IF EXISTS (SELECT 1 FROM [master].[dbo].[sysdatabases] WHERE [name] = N'$(DatabaseName)') BEGIN ALTER DATABASE [$(DatabaseName)] SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF), CONTAINMENT = PARTIAL WITH ROLLBACK IMMEDIATE; END Error SQL72014: .Net SqlClient Data Provider: Msg 5069, Level 16, State 1, Line 5 ALTER DATABASE statement failed. Error SQL72045: Script execution error. The executed script: IF EXISTS (SELECT 1 FROM [master].[dbo].[sysdatabases] WHERE [name] = N'$(DatabaseName)') BEGIN ALTER DATABASE [$(DatabaseName)] SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF), CONTAINMENT = PARTIAL WITH ROLLBACK IMMEDIATE; END (Microsoft.SqlServer.Dac)