Featured image of post SQL Server Logins

SQL Server Logins

Configuring Logins in SQL Server

Introduction

Configuring logins in SQL Server is one of the essential tasks for making sure access to the database is properly controlled. There are two main kinds of logins in SQL Server: SQL Server logins and Windows logins. In this post, we’ll look at how to set up both kinds of logins and go over recommended practices for managing user access.

1. Creating SQL Server Logins

In SQL Server, you can create logins through Management Studio or with T-SQL. Let’s start with a basic example of how to create a SQL Server login:

1
CREATE LOGIN NovoUsuario WITH PASSWORD = 'SenhaSegura123';

This command creates a login named NovoUsuario with an associated password. After creating it, you need to grant permissions to this login. That’s done by associating it with a user account inside a specific database.

Example of creating a login with password expiration settings:

1
2
3
4
CREATE LOGIN NovoUsuario
WITH PASSWORD = 'SenhaSegura123',
     CHECK_POLICY = ON,
     CHECK_EXPIRATION = ON;

In this example, the password policy and password expiration are enabled, forcing the user to change the password after the set period.

2. Creating Windows Logins

You can also create Windows-based logins, letting users access the database with their operating system credentials:

1
CREATE LOGIN [DOMINIO\Usuario] FROM WINDOWS;

In this case, the login is created using the Windows account DOMINIO\Usuario.

Windows Logins vs SQL Server Logins

  • SQL Server logins: Useful in scenarios where users aren’t part of a domain, or when you need authentication that is exclusive to SQL Server.
  • Windows logins: Recommended when domain security and user account administration are already handled at the operating system level.

3. Mapping Logins to Databases

After creating the login, the next step is to map it to a database. That’s done by creating a database user for the login:

1
2
USE MeuBancoDeDados;
CREATE USER NovoUsuario FOR LOGIN NovoUsuario;

The command above creates a user in the MeuBancoDeDados database mapped to the NovoUsuario login.

Example of mapping a Windows login:

1
2
USE MeuBancoDeDados;
CREATE USER [DOMINIO\Usuario] FOR LOGIN [DOMINIO\Usuario];

4. Granting Permissions

After mapping the login to the database, you need to grant the required permissions. For example, to grant read and write permissions:

1
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO NovoUsuario;

This gives the NovoUsuario user read and write permissions on every table in the dbo schema.

Example of granting permissions to Windows logins:

1
GRANT EXECUTE ON SCHEMA::dbo TO [DOMINIO\Usuario];

In this case, the Windows login [DOMINIO\Usuario] gets permission to execute stored procedures in the dbo schema.

5. Security Considerations

  • Strong passwords: Always use strong passwords for SQL Server logins. Weak passwords are one of the main attack vectors.
  • Least privilege: Avoid logins with administrative permissions unless absolutely necessary. Follow the principle of “least privilege”.
  • Windows logins: Whenever possible, use Windows logins, because they take advantage of the operating system’s security and authentication controls.
  • Auditing: It’s important to set up security auditing to monitor login activity, such as failed login attempts and logins with elevated privileges.

SQL Server Logins Configuration

Conclusion

Configuring logins in SQL Server is a fundamental task for ensuring security and access control over your data. By creating the right logins and applying security best practices, such as strong passwords and minimal permissions, you protect your database environment against unauthorized access. It’s also important to review permissions regularly and make sure logins are kept up to date and in line with your organization’s security policies.

Built with Hugo
Theme Stack designed by Jimmy