简体   繁体   English

以编程方式将用户添加到以编程方式创建的数据库 (Azure SQL)

[英]Programmatically add user to programmatically created database (Azure SQL)

I'm using Azure Management ResourceManager to programmatically create a database on an Azure SQL Server.我正在使用 Azure Management ResourceManager 以编程方式在 Azure SQL 服务器上创建数据库。 This works fine.这工作正常。 I use:我用:

var azureCredentials = new AzureCredentials(new ServicePrincipalLoginInformation
{
    ClientId = ClientID,
    ClientSecret = ClientSecret,
}, TenantID, AzureEnvironment.AzureGlobalCloud);

var newDB = azure.SqlServers.Databases.Define(DatabaseName)
    .WithExistingSqlServer(ResourceGroupName, SqlServerName, Region.EuropeWest.Name)
    .WithEdition(DatabaseEdition.Standard)
    .WithServiceObjective(ServiceObjectiveName.S1)                    
    .Create();

Now I want to add a login to the master database, and a user based on that login to the created database.现在我想添加一个登录到主数据库,以及一个基于该登录的用户到创建的数据库。
However, I seem to be unable to do this completely programmatically.但是,我似乎无法完全以编程方式做到这一点。
I have an app registration that I use to get a token, but when I try to create ac connection with an SqlConnection using the token, I get an error:我有一个用于获取令牌的应用程序注册,但是当我尝试使用令牌创建与 SqlConnection 的交流连接时,出现错误:
Login failed for user '<token-identified principal>'.

According to the Microsoft documentation I first need to add the user to the database, using T-SQL:根据Microsoft 文档,我首先需要使用 T-SQL 将用户添加到数据库:

CREATE USER [<Azure_AD_principal_name>] FROM EXTERNAL PROVIDER;

But I can't do this programmatically without having a connection to the database first!但是如果不先连接到数据库,我就无法以编程方式执行此操作!

Is there no way to do this completely automated??有没有办法完全自动化?

Cheers, CJ干杯,CJ

I got so caught up in app registrations and tokens, I completely forgot that I also have a server admin account for the databaser server.. Stupid me...我被应用程序注册和令牌迷住了,我完全忘记了我还有一个用于数据库服务器的服务器管理员帐户.. 愚蠢的我......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM