System.ArgumentException: ‘authentication’ 键的值无效。

huangapple go评论74阅读模式
英文:

System.ArgumentException: 'Invalid value for key 'authentication'

问题

我有一个运行在 .NET 4.6.1 上的 web 应用,托管在 Azure 应用服务中,它通过使用实体框架连接到 Azure SQL Server,使用 SQL Server 身份验证(用户名/密码),继承自 System.Data.Entity 命名空间中的 DbContext 类。

我计划使用 Active Directory 服务主体身份验证模式。因此,我修改了我的连接字符串如下:

Data Source= servername.database.windows.net; Encrypt=True; 
Initial Catalog= dbname; Authentication=Active Directory Service Principal; 
User Id=servicePrincipalID; Password=servicePrincipalPassword;

当尝试打开连接时,这个修改后的字符串会抛出错误:

> System.ArgumentException: 'authentication' 键的值无效。

许多在线文章/资源都清楚地介绍了如何在 .NET Core 应用程序中集成,但对于运行在 .NET Framework 上的应用程序,却没有太多的信息。

请告诉我们如果您能为我们提供指导。

英文:

I have a web app (running on .NET 4.6.1) hosted in an Azure App Service which connects to an Azure SQL Server via SQL Server authentication (username/password) using Entity Framework, inheriting from DbContext class from System.Data.Entity namespace.

I'm planning to use Active Directory Service Principal authentication mode. Due to this I modified my connection string to

Data Source= servername.database.windows.net; Encrypt=True; 
Initial Catalog= dbname; Authentication=Active Directory Service Principal; 
User Id=servicePrincipalID; Password=servicePrincipalPassword;

This modified string throws an error

> System.ArgumentException: 'Invalid value for key 'authentication'.'

when trying to open the connection. Many online articles/resources have clear integration for .NET core apps using the Microsoft.EntityFrameworkCore assembly, however nothing much for apps running on the .NET Framework.

Please let us know if you can guide us.

答案1

得分: 1

Microsoft文档中指出连接字符串应该构建如下:

string ConnectionString = @"Server=demo.database.windows.net; Authentication=Active Directory Service Principal; Encrypt=True; Database=testdb; User Id=AppId; Password=secret";
    
using (SqlConnection conn = new SqlConnection(ConnectionString)) {
    conn.Open();
}

请注意,此类型的身份验证需要 Microsoft.Data.SqlClient 版本为 2.0.0+,适用于 Entity Framework 4.6+。

此外,您需要在Azure SQL数据库中创建一个与服务主体标识相对应的用户。例如:

create user [servicePrincipalID] from external provider;

最后,请确保设置所需的应用程序注册

英文:

On Microsoft Documentation states the connection string should be build as shown below:

string ConnectionString = @"Server=demo.database.windows.net; Authentication=Active Directory Service Principal; Encrypt=True; Database=testdb; User Id=AppId; Password=secret";

using (SqlConnection conn = new SqlConnection(ConnectionString)) {
    conn.Open();
}

Please note that this type of authentication requires Microsoft.Data.SqlClient version 2.0.0+ on Entity Framework 4.6+.

In addition, you need to create a user in your Azure SQL Database that corresponds to the service principal identity. For example:

create user [servicePrincipalID] from external provider;

Finally, make sure you setup the required app registration.

huangapple
  • 本文由 发表于 2023年7月27日 19:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定