Create user in Azure SQL database using Azure functions?

隐身守侯 提交于 2020-04-18 03:40:32

问题


I want to create users in Azure SQL database. I am running this PowerShell through Azure Functions.

I have set up my own name as server admin. Also I have configured the firewall.

But I am not able to connect to SQL database.

It gives me error : Cannot open server "domain.com" requested by the login. The login failed.

This is my script:

$serverName = "servername.database.windows.net"

$databaseName = "databasename"

$adminLogin = 'user@domain.com'

$PWord = "password"

$query = "CREATE USER [user1@domain.com] FROM EXTERNAL PROVIDER;";

Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -U $adminLogin -Password $PWord -Query $query

I have tried this script as well:

Although below script works in Windows PowerShell, but does not work in Azure functions PowerShell.

$Creds = Get-Credential -Credential 'username@domain.com'
    $Username = $($Creds.GetNetworkCredential().UserName)
    $Password = $($Creds.GetNetworkCredential().Password)
    $Database = "testg"
    $Server = 'test.database.windows.net'
    $Port = 1433
    $cxnString = "Server=tcp:$Server,$Port;Database=$Database;Authentication=Active Directory Password;UID=$UserName;PWD=$Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
    $query = "CREATE USER YourUser WITH PASSWORD = 'JRxuerCc(q'"
    $cxn = New-Object System.Data.SqlClient.SqlConnection($cxnString)
    $cxn.Open()
    $cmd = New-Object System.Data.SqlClient.SqlCommand($query, $cxn)
    $cmd.CommandTimeout = 120
    $cmd.ExecuteNonQuery()
    $cxn.Close()

But this does not work in Azure functions. It say keyword not supported: 'authentication'

Similar way I tried with .NET approach as well. But it gives the same error

How can I achieve this?


回答1:


Finally I found answer to this. If anybody still searching for answer here it is

This is currently not possible with PowerShell in azure functions.

I had to do this with .net core.

key here is to use Microsoft.Data.SqlClient package. System.Data.SqlClient does not support active directory password authentication.

Use this Microsoft.Data.SqlClient package and bingo!

code goes here :

using Microsoft.Data.SqlClient;

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

            string userName = "username@domain.com";
            string password = "password";
            builder.ConnectionString = "Server=tcp:" + server + ",1433;Database=" + database + ";Authentication=Active Directory Password;UID=" + userName + ";PWD=" + password + ";Trusted_Connection=False;Encrypt=True;Connection Timeout=30;";

            using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
            {
                connection.Open();
                string query = "CREATE USER YourUser WITH PASSWORD = 'JRxuerCc(q'"
                SqlCommand cmd = new SqlCommand(query, connection);
                cmd.CommandTimeout = 120;
                cmd.ExecuteNonQuery();
                connection.Close();
            }


来源:https://stackoverflow.com/questions/61222418/create-user-in-azure-sql-database-using-azure-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!