1
Vote

SQL Server Create User with special characters - ID: 3008498

description

When creating a user with periods, and possibly other special characters, the create fails.
 
Please modify the create user method to include brackets around the user and database in the sql statement...
 
public virtual void CreateUser(SqlUser user, string password)
{
if (user.Databases == null)
user.Databases = new string[0];
 
// create user account
if (user.DefaultDatabase == null || user.DefaultDatabase == "")
user.DefaultDatabase = "master";
 
//ExecuteNonQuery(String.Format("EXEC sp_addlogin '{0}', '{1}', '{2}'",
// user.Name, password, user.DefaultDatabase));
//Fixed create login with "Enforce password policy" disabled.
ExecuteNonQuery(
String.Format("CREATE LOGIN [{0}] WITH PASSWORD='{1}', DEFAULT_DATABASE=[{2}], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF",
user.Name, password, user.DefaultDatabase));
 
// add access to databases
foreach (string database in user.Databases)
AddUserToDatabase(database, user.Name);
}

comments