如何在RegisteredServer中指定身份验证

基于我有关在microsoft.SqlServer.Management.RegisteredServers中创建RegisteredServer的问题, Unable to create RegisteredServer in Microsoft.SqlServer.Management.RegisteredServers,我致力于从Azure SQL Server添加数据库。我可以添加数据库,但是身份验证基于Windows Authentication

如何在RegisteredServer中指定身份验证

当然,我无法连接到Azure:

如何在RegisteredServer中指定身份验证

因此,我正在寻找一种在C# code中指定身份验证的解决方案。奇怪是因为我用用户名和密码传递了连接字符串:

        var registeredServer = new RegisteredServer(serverName)
        {

            ConnectionString = "Server=tcp:server.database.windows.net,1433;Initial Catalog=...;Persist Security Info=true;User ID=...;Password=....;MultipleactiveResultSets=True;Encrypt=True;TrustServerCertificate=False;"
        }
liubing065 回答:如何在RegisteredServer中指定身份验证

首先,Azure SQL supports AAD authentication。据我所知,Windows身份验证适用于本地SQL Server。

enter image description here

而且,您需要set AAD admin for your Azure SQL server

enter image description here

之后,您可以使用AAD凭据连接到Azure SQL。如果您的本地帐户已通过AD Connect同步到Azure AD。然后,您也应该能够使用AD凭据连接到Azure SQL。

,

最后,我解决了它,我们需要安装System.Security.Cryptography.ProtectedData软件包才能设置连接字符串:

 Install-Package System.Security.Cryptography.ProtectedData -Version 4.6.0

然后,我们需要像这样指定CredentialPersistenceType和AuthenticationType:

        var registeredServer = new RegisteredServer(databaseEngineServerGroup,serverName)
        {
            ConnectionString = localConnectionString,Description = $"This is a sample connection to {serverName}",CredentialPersistenceType = CredentialPersistenceType.PersistLoginNameAndPassword,AuthenticationType = 1,};
本文链接:https://www.f2er.com/3104387.html

大家都在问