首页 > 解决方案 > 通过 Visual Studio 使用 AD auth 设置默认 azure Web 应用程序返回错误

问题描述

我想创建一个使用 Azure AD 进行身份验证的简单应用程序。

我决定通过 Visual Studio 做到这一点,所以我:

  1. 创建了一个启用了工作或学校帐户身份验证的新 MVC 项目。这将创建示例应用程序并在我的租户中设置应用程序注册。
  2. 将应用程序发布到 Azure。这将创建 Web 应用程序并将其托管在带有 .azurewebsites.net 域的 azure 中。
  3. 调整 AD 租户中应用注册的回复 URL 以适合我的应用。

为了清楚起见,这里是我的 web.config(生成):

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-xxx-20180626122432.mdf;Initial Catalog=aspnet-xxx-20180626122432;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="ida:ClientId" value="e9xx-xxx-xxxf-9xxd-xxx" />
    <add key="ida:AADInstance" value="https://login.microsoftonline.com/" />
    <add key="ida:ClientSecret" value="xxxA=" />
    <add key="ida:Domain" value="xxx.onmicrosoft.com" />
    <add key="ida:TenantId" value="xxx-fxxa-xxe7-9xxx-xxx" />
    <add key="ida:PostLogoutRedirectUri" value="https://xxx.azurewebsites.net" />
  </appSettings>

为什么它甚至会生成连接字符串?我不想要数据库,这似乎也是导致我得到错误的原因。

据我所知应该是这样。但是当我登录到我的网站时,我收到以下错误:

[Win32Exception (0x80004005): 系统找不到指定的文件]

[SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:SQL 网络接口,错误:52 - 无法找到本地数据库运行时安装。验证 SQL Server Express 已正确安装并且本地数据库运行时功能已启用。)]
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 标识,SqlConnectionString connectionOptions,SqlCredential 凭据,对象 providerInfo,String newPassword,SecureString newSecurePassword,布尔重定向用户实例,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData,DbConnectionPool 池,字符串 accessToken,布尔 applyTransientFaultHandling)+821
系统.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +332
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +699
System. Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +89
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +426
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource 1 重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& 连接)+191 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 重试,DbConnectionOptions userOptions ) +21 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 次重试) +209 System.Data.SqlClient.SqlConnection.Open() +96 System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) +101 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource

1 retry, DbConnectionOptions userOptions) +154
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource

1 retry) +90
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource


System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch(TTarget target, Action2 操作,TInterceptionContext interceptionContext,Action 3 executing, Action3 执行) +72
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +360
System.Data.Entity .SqlServer.<>c__DisplayClass33.b__32() +426 System.Data.Entity.SqlServer.<>c__DisplayClass1.b__0() +10
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func 1 act) +175 System.Data .Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action 1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +901 operation) +189
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +78
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action

1 act) +552
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable

System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection 连接,Nullable 1 commandTimeout,StoreItemCollection storeItemCollection)+76 System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +134 System.Data.Entity.Migrations.Utilities .DatabaseCreator.Create(DbConnection 连接)+119 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase)+142 System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration)+78 System.Data.Entity。 Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func 1.InitializeDatabase(TContext context) +151 System.Data.Entity.Internal.<>c__DisplayClassf1 commandTimeout, StoreItemCollection storeItemCollection) +171
System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable





3 createMigrator, ObjectContext objectContext) +89 System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +116
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +218
System.Data.Entity.CreateDatabaseIfNotExists

1.<CreateInitializationAction>b__e() +76 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +60
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +357 System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7
System.Data.Entity.Internal.RetryAction
1.PerformAction(TInput 输入) +110 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action 1.Initialize() +53 System.Data.Entity.Internal.Linq.InternalSet 1.System.Linq.IQueryable.get_Provider( ) +38 System.Linq.Queryable.FirstOrDefault(IQueryable 1 谓词) +83 AdAuthPocDamco.Models.ADALTokenCache..ctor(String signedInUserId) 在 c:\users\manielse\source\repos\AdAuthPocDamco\AdAuthPocDamco\Models\AdalTokenCache.cs :26 AdAuthPocDamco.Startup.b__7_0(AuthorizationCodeReceivedNotification context) 在 c:\users\manielse\source\repos\AdAuthPocDamco\AdAuthPocDamco\App_Start\Startup.Auth.cs:54 Microsoft.Owin.Security.OpenIdConnect.d__1a.MoveNext() + 4952 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+261 action) +198
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73 System.Data.Entity.Internal.InternalContext.Initialize() +30 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +16
System.Data.Entity.Internal.Linq.InternalSet

1.get_InternalContext() +15 System.Data.Entity.Infrastructure.DbQuery1 source, Expression

Microsoft.Owin.Security.OpenIdConnect.d__1a.MoveNext() +6433 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin.Security .Infrastructure.d__0.MoveNext() +571 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin.Security.Infrastructure.d__0。 MoveNext() +255
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext() +182 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin .Security.Infrastructure.d__0.MoveNext() +638
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin.Host.SystemWeb。 IntegratedPipeline.d__5.MoveNext() +182 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext() +180 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork( IAsyncResult ar) +64
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously ) +159

标签: c#.netazureazure-active-directory

解决方案


为什么它甚至会生成连接字符串?

我无法重现您提到的问题。在我的 web.config 文件中,没有创建数据库连接字符串。它在我这边正常工作。

我不想要数据库,这似乎也是导致我得到错误的原因。

是的你是对的。由于默认情况下 Azure WebApp 无法连接到本地数据库。

如果不需要数据库,可以在发布到 Azure WebApp 期间删除 SQL 连接。或者,您可以创建另一个 Web 项目并再次尝试。


推荐阅读