Dot-Net

Azure:使用者登錄失敗

  • December 26, 2021

我做了什麼:

  • 我在 Azure 中添加了資源組。
  • 使用以前的資源組添加了 Web 服務。
  • 使用以前的資源組添加了 SQL Server 數據庫。
  • 從該數據庫中檢索生成的連接字元串,並在此處提供使用者名和密碼。
  • 將此連接字元串添加到 Web 服務中的應用程序設置。
  • 通過 Visual Studio 發布的應用程序。

問題:當我嘗試在 azure 中測試此託管應用程序時出現此錯誤,但在我的本地應用程序版本和此連接字元串上,一切正常。

Server Error in '/' Application.

Login failed for user 'USERNAME'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'USERNAME'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SqlException (0x80131904): Login failed for user 'USERNAME'.]
  System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) +1418
  System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +470
  System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +70
  System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +777
  System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +114
  System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1637
  System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +117
  System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +267
  System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +318
  System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +132
  System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +246
  System.Data.SqlClient.SqlConnection.Open() +122
  System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +104
  System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +509
  System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +15
  System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +238
  System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act) +331
  System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +916
  System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +413
  System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +116

[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
  System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +377
  System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +35

[ProviderIncompatibleException: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.]
  System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +266
  System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +84
  System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +89
  System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +63
  System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +91
  System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +227
  System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +846
  Hakon.HamiltonCargo.Web.NinjectWebCommon.InitializeDatabase() +111
  Hakon.HamiltonCargo.Web.NinjectWebCommon.Start() +124

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
  System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
  System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +260
  System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
  System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +34
  WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +288
  WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +785
  WebActivatorEx.ActivationManager.RunPreStartMethods(Boolean designerMode) +58
  WebActivatorEx.ActivationManager.Run() +82

[InvalidOperationException: The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
  System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +877
  System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +169
  System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +133
  System.Web.Compilation.BuildManager.ExecutePreAppStart() +176
  System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +734

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivatorEx.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
  System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +539
  System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +125
  System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +731

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1590.0

問題是 Microsoft 生成的連接字元串中有兩個鍵。您必須設置為“Persist Security Info”和“TrustServerCertificate”鍵值:“True”。

在我的情況下,net core 3.1,我試圖通過 連接提供連接字元串Configuration.GetConnectionString,該字元串儲存在appsettings.json中,密碼具有 unicode 值,並且 json 文件以 UTF-8 以外的編碼保存。所以,為了解決這個問題,我不得不將 json 文件保存為 UTF-8 編碼並且它有效。

引用自:https://stackoverflow.com/questions/43260500