使用 Microsoft Identity Framework 时出现数据库连接问题
DB Connection Problem while using Microsoft Identity Framework
收到以下错误“建立与 SQL 服务器的连接时发生与网络相关或特定于实例的错误”,使用 .NET 4.5。在我的本地 IIS 7.0 实例上部署了 Web 应用程序。
正在尝试对应用程序进行本地设置并尝试连接到远程数据库服务器。以下是使用 Microsoft 身份框架检查登录凭据的代码。
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(Login1.UserName, Login1.Password);
但我能够使用 SQL Server Management Studio 连接到远程数据库。
一点帮助将非常有用。以下是堆栈跟踪
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +353
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +118
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +268
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +315
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +128
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +265
System.Data.SqlClient.SqlConnection.Open() +133
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) +485
System.Data.Entity.SqlServer.<>c__DisplayClass33.<UsingConnection>b__32() +560
System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +18
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +230
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +914
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +117
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +211
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +128
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +156
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +116
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +124
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +292
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +187
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +75
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +482
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +177
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +272
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +69
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +66
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken) +209
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate) +172
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +498
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +62
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +43
Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +353
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +62
Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +348
Website.Login.Login_Click(Object sender, ImageClickEventArgs e) in C:\Work\www\web\mysite\Login.aspx.cs:38
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +143
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +188
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1964
我在我的代码中覆盖了 DefaultConnection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
namespace MyWebsite
{
public class MyApplicationDBContext : IdentityDbContext
{
public MyApplicationDBContext() : base("DefaultConnection") { }
}
}
然后修改了配置文件
<connectionStrings>
<add name="DefaultConnection" connectionString="data source=serverip,portnumber;initial catalog=ICE;user id=uid;password=pwd;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>
发生这种情况是因为,默认连接是在 .NET 配置文件 (machine.config) 中配置的,因此 IIS 仅采用此配置文件。通过在代码中覆盖它是我经过很长时间搜索后得到的最佳解决方案。
收到以下错误“建立与 SQL 服务器的连接时发生与网络相关或特定于实例的错误”,使用 .NET 4.5。在我的本地 IIS 7.0 实例上部署了 Web 应用程序。
正在尝试对应用程序进行本地设置并尝试连接到远程数据库服务器。以下是使用 Microsoft 身份框架检查登录凭据的代码。
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(Login1.UserName, Login1.Password);
但我能够使用 SQL Server Management Studio 连接到远程数据库。
一点帮助将非常有用。以下是堆栈跟踪
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +353
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +118
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +268
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +315
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +128
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +265
System.Data.SqlClient.SqlConnection.Open() +133
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) +485
System.Data.Entity.SqlServer.<>c__DisplayClass33.<UsingConnection>b__32() +560
System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +18
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +230
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +914
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +117
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +211
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +128
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +156
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +116
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +124
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +292
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +187
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +75
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +482
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +177
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +272
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +69
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +66
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken) +209
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable`1 source, Expression`1 predicate) +172
Microsoft.AspNet.Identity.EntityFramework.<GetUserAggregateAsync>d__6c.MoveNext() +498
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +62
Microsoft.AspNet.Identity.CultureAwaiter`1.GetResult() +43
Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +353
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +32
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +62
Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +348
Website.Login.Login_Click(Object sender, ImageClickEventArgs e) in C:\Work\www\web\mysite\Login.aspx.cs:38
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +143
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +188
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1964
我在我的代码中覆盖了 DefaultConnection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
namespace MyWebsite
{
public class MyApplicationDBContext : IdentityDbContext
{
public MyApplicationDBContext() : base("DefaultConnection") { }
}
}
然后修改了配置文件
<connectionStrings>
<add name="DefaultConnection" connectionString="data source=serverip,portnumber;initial catalog=ICE;user id=uid;password=pwd;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>
发生这种情况是因为,默认连接是在 .NET 配置文件 (machine.config) 中配置的,因此 IIS 仅采用此配置文件。通过在代码中覆盖它是我经过很长时间搜索后得到的最佳解决方案。