简体   繁体   English

在 azure sql 上连接到数据库的问题

[英]Problems connecting to database on azure sql

I'm trying do do the update-database on Entity Framework Core, which gives me this error.我正在尝试在 Entity Framework Core 上执行更新数据库,这给了我这个错误。

Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user 'hxxxxxxxo@hxxxxxxxogmail.onmicrosoft.com'.
   at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:429fba14-5656-4560-9cb8-efb35ab0dc34
Error Number:18456,State:1,Class:14
Login failed for user 'hxxxxxxxo@hxxxxxxxogmail.onmicrosoft.com'.

I can connect with SSMS with this password and connect to the database, I have other API and I can connect to it, I don't know what happens.我可以用这个密码连接SSMS并连接到数据库,我有其他API并且我可以连接到它,我不知道会发生什么。

services.AddDbContext<AAAADbContext>(options => 
  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnectionString")));

Connection String连接字符串

"DefaultConnectionString": "Server=tcp:xxxx-xxxxxxxx.database.windows.net,1433;Initial Catalog=xxxxxxxxxxxDb;Persist Security Info=False;User ID= ;Password= ;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=Active Directory Password"

More information on MSSQLSERVER_18456 :有关MSSQLSERVER_18456 的更多信息:

When a connection attempt is rejected because of an authentication failure that involves a bad password or user name, a message similar to the following is returned to the client: "Login failed for user '<user_name>'. (Microsoft SQL Server, Error: 18456)".当连接尝试因涉及错误密码或用户名的身份验证失败而被拒绝时,将向客户端返回类似于以下内容的消息:“用户 '<user_name>' 登录失败。(Microsoft SQL Server,错误: 18456)”。

More information on State 1:有关状态 1 的更多信息:

Error information is not available.错误信息不可用。 This state usually means you do not have permission to receive the error details.这种状态通常意味着您无权接收错误详细信息。 Contact your SQL Server administrator for more information.有关详细信息,请联系您的 SQL Server 管理员。

It seems like this all boils down to either a bad username/password, or misconfiguration of the user/database.似乎这一切都归结为错误的用户名/密码或用户/数据库的错误配置。

EDIT:编辑:

Have you looked at using a Managed Identity to connect to your database?您是否考虑过使用托管身份连接到您的数据库?

App Service provides a highly scalable, self-patching web hosting service in Azure.应用服务在 Azure 中提供高度可扩展、自修补的 Web 托管服务。 It also provides a managed identity for your app, which is a turn-key solution for securing access to Azure SQL Database and other Azure services.它还为您的应用程序提供托管标识,这是用于保护对Azure SQL 数据库和其他 Azure 服务的访问的交钥匙解决方案。 Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the connection strings.应用服务中的托管身份通过消除应用中的机密(例如连接字符串中的凭据)来提高应用的安全性。 In this tutorial, you will add managed identity to the sample web app you built in one of the following tutorials:在本教程中,您将向您在以下教程之一中构建的示例 Web 应用程序添加托管标识:

  • Tutorial: Build an ASP.NET app in Azure with Azure SQL Database教程:使用 Azure SQL 数据库在 Azure 中构建 ASP.NET 应用
  • Tutorial: Build an ASP.NET Core and Azure SQL Database app in Azure App Service教程:在 Azure 应用服务中构建 ASP.NET Core 和 Azure SQL 数据库应用

Source: Tutorial: Secure Azure SQL Database connection from App Service using a managed identity来源: 教程:使用托管标识保护来自应用服务的 Azure SQL 数据库连接

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

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