Removing the remembered login and password list in SQL Server Management Studio

前端 未结 10 3134
一生所求
一生所求 2020-11-30 17:18

I\'ve recently used our company\'s spare laptop (that has a general user set up) while mine was being repaired. I\'ve checked the \"Remember password\" option in SQL Server

10条回答
  •  情深已故
    2020-11-30 18:04

    In my scenario I only wanted to remove a specific username/password from the list which had many other saved connections I didn't want to forget. It turns out the SqlStudio.bin file others are discussing here is a .NET binary serialization of the Microsoft.SqlServer.Management.UserSettings.SqlStudio class, which can be deserialized, modified and reserialized to modify specific settings.

    To accomplish removal of the specific login, I created a new C# .Net 4.6.1 console application and added a reference to the namespace which is located in the following dll: C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\Microsoft.SqlServer.Management.UserSettings.dll (your path may differ slightly depending on SSMS version)

    From there I could easily create and modify the settings as desired:

    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using Microsoft.SqlServer.Management.UserSettings;
    
    class Program
    {
        static void Main(string[] args)
        {
            var settingsFile = new FileInfo(@"C:\Users\%username%\AppData\Roaming\Microsoft\SQL Server Management Studio\13.0\SqlStudio.bin");
    
            // Backup our original file just in case...
            File.Copy(settingsFile.FullName, settingsFile.FullName + ".backup");
    
            BinaryFormatter fmt = new BinaryFormatter();
    
            SqlStudio settings = null;
    
            using(var fs = settingsFile.Open(FileMode.Open))
            {
                settings = (SqlStudio)fmt.Deserialize(fs);
            }
    
            // The structure of server types / servers / connections requires us to loop
            // through multiple nested collections to find the connection to be removed.
            // We start here with the server types
    
            var serverTypes = settings.SSMS.ConnectionOptions.ServerTypes;
    
            foreach (var serverType in serverTypes)
            {
                foreach (var server in serverType.Value.Servers)
                {
                    // Will store the connection for the provided server which should be removed
                    ServerConnectionSettings removeConn = null;
    
                    foreach (var conn in server.Connections)
                    {
                        if (conn.UserName == "adminUserThatShouldBeRemoved")
                        {
                            removeConn = conn;
                            break;
                        }
                    }
    
                    if (removeConn != null)
                    {
                        server.Connections.RemoveItem(removeConn);
                    }
                }
            }
    
            using (var fs = settingsFile.Open(FileMode.Create))
            {
                fmt.Serialize(fs, settings);
            }
        }
    }
    

提交回复
热议问题