CREATE DATABASE权限在以SA身份访问数据库“ master”时被拒绝

时间:2019-01-28 20:27:19

标签: sql-server-express

我创建了一个用于测试目的的小型应用程序。当我尝试将应用程序部署到Azure VM时,问题开始。访问已发布的应用程序时,出现错误

  

数据库'master'中的CREATE DATABASE权限被拒绝

奇怪的是,当我从PC或SSMS用相同的用户(sa)访问相同的DB时,它可以正常工作。它仅在Web服务器上运行时才起作用。我不知道怎么了。

这是我得到的堆栈跟踪:

[SqlException (0x80131904): CREATE DATABASE permission denied in database 'master'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +3306108
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +736
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4061
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +1293
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +421
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +380
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +104
   System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +499
   System.Data.Entity.SqlServer.<>c__DisplayClass1a.<CreateDatabaseFromScript>b__19(DbConnection conn) +135
   System.Data.Entity.SqlServer.<>c__DisplayClass33.<UsingConnection>b__32() +587
   System.Data.Entity.SqlServer.<>c__DisplayClass1.<Execute>b__0() +18
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +234
   System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act) +909
   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) +125
   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) +269
   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() +59
   System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate) +61
   MajowskiMartinCloud.Models.ADALTokenCache..ctor(String signedInUserId) +539
   MajowskiMartinCloud.Startup.<ConfigureAuth>b__7_0(AuthorizationCodeReceivedNotification context) +149
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +5428
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +5937
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__0.MoveNext() +817
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +329
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +197
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +768
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +197
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<DoFinalWork>d__2.MoveNext() +184
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +117
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +367
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

1 个答案:

答案 0 :(得分:0)

两天后,我终于发现女巫出了问题。

当我在VS2015中创建项目时,我创建了一个具有AzureAD身份验证的新MVC项目。创建解决方案时,VS将以下部分代码添加到Web配置中以管理令牌缓存:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

在我的PC上,我使用的是IIS Express服务器,因此我假定VS正在管理所有授权部分。而且我只是不关心它,因为它正在工作,所以我什至不知道除了我为EF模型定义的访问之外,还有另一个DB访问。

我不确定,但是我假设使用此配置,应用程序尝试使用IIS用户连接到本地sql服务器实例(。\ SQLEXPRESS)。 IIS用户对我的Sql实例没有任何作用,因此我认为这是错误的原因。我决定通过以下方式更改上面的部分:

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;MultipleActiveResultSets=True;User ID=*******;Password=*******" />
  </parameters>
</defaultConnectionFactory>

现在可以使用