我在Visual Studio 2015中创建了一个新的ASP.NET Core Web应用程序.我还设置了一个Azure Web应用程序,以便从GitHub中提取应用程序并运行它.这工作正常,但我无法连接到Azure上的数据库.
在本地,这是有效的,它使用config.json和代码Data:DefaultConnection:ConnectionString连接字符串.
如何保留代码,并使其在Azure中运行?我尝试在门户中设置应用程序设置,包括连接字符串和应用程序设置.并使用"SQLCONNSTR_DefaultConnection"和"Data:DefaultConnection:ConnectionString"作为键.
(设置应用程序设置似乎不起作用.我认为我提供的值太长).
那么如何在我的Azure Web应用程序(ASP.NET 5)中提供Azure数据库的连接字符串,而不在源代码管理中检入它?
Update My Startup.cs看起来像这样(参见GitHub上的完整文件):
public Startup(IHostingEnvironment env)
{
var configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
configuration.AddUserSecrets();
}
configuration.AddEnvironmentVariables();
Configuration = configuration;
}
Run Code Online (Sandbox Code Playgroud)
在该ConfigureServices方法中,还有:
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
Run Code Online (Sandbox Code Playgroud)
而且,在ConfigureServices方法中:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
.AddDbContext<InvoicesDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// So this is where I want my app in Azure to use the connection string I
// provide in the …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MSI从控制台应用程序连接到Azure Vault
对于此保管库,我已将我的用户添加为“选择的原理”
,我用来连接的代码是
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("https://<vaultname>.vault.azure.net/secrets/<SecretName>").ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
我得到以下异常
Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException:参数:Connectionstring:[未指定连接字符串],资源:https : //vault.azure.net,颁发机构