【发布时间】:2015-08-07 02:50:53
【问题描述】:
首先,我已经在这里检查过: ASP.Net Identity how to set target DB?
我现在收到此错误
数据库“master”中的 CREATE DATABASE 权限被拒绝。
在这行代码上:
Dim user As User = manager.Find(Trim(Username.Text), Trim(Password.Text))
完全错误:
[SqlException (0x80131904): 数据库“主”中的 CREATE DATABASE 权限被拒绝。]
System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection,Action1 wrapCloseInAction) +32498521 完成,字符串方法名,布尔型 sendToPipe,Int32 超时,布尔型 asyncWrite)+367
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +345
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4927
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +1287
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +386
System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch(TTarget target, Func3 操作,TInterceptionContext 拦截上下文,Action3 executing, Action3 执行)+965
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +505
System.Data.Entity.SqlServer.c__DisplayClass1a.b__19(DbConnection conn) +136
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action1 act) +3471 act) +916
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +1171 commandTimeout, StoreItemCollection storeItemCollection) +212
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection 连接) +172
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase)+175
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +116
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func3 createMigrator, ObjectContext objectContext) +1211.InitializeDatabase(TContext context) +257
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +169
System.Data.Entity.CreateDatabaseIfNotExists
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +483
System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +1771 操作)+274
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +37
System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +761.get_InternalContext() +21
System.Data.Entity.Internal.Linq.InternalSet
System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +591 源,Expression
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable1 predicate, CancellationToken cancellationToken) +2081 源,Expression`1 谓词)+172
System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryableMicrosoft.AspNet.Identity.EntityFramework.d__6c.MoveNext() +502
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+13855856
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+61
Microsoft.AspNet.Identity.CultureAwaiter1.GetResult() +481 func) +348
Microsoft.AspNet.Identity.<FindAsync>d__12.MoveNext() +357
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13855856
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func
C:\MyApp\Login_identity.aspx.vb:168 中的 MyApp.Login_identity.UserLogin_Click(Object sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +11747645
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +150
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3360
我将AppModel.vb 中的DbContext 初始化更改为我现有的连接字符串conn1,它指向我已经转换为新标识表的SQL Server 数据库。
我的连接字符串:
<add name="conn1"
connectionString="data source=(local)\sqlexpress;Initial Catalog=myapp;User Id=sa;Password=XXXX;"
providerName="System.Data.SqlClient" />
AppModel.vb:
Imports Microsoft.AspNet.Identity
Imports Microsoft.AspNet.Identity.EntityFramework
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports System.Data.Entity
Imports System.Linq
Imports System.Web
Namespace AspnetIdentitySample.Models
Public Class ApplicationUser
Inherits IdentityUser
' HomeTown will be stored in the same table as Users
Public Property HomeTown() As String
Get
Return m_HomeTown
End Get
Set(value As String)
m_HomeTown = Value
End Set
End Property
Private m_HomeTown As String
Public Overridable Property ToDoes() As ICollection(Of ToDo)
Get
Return m_ToDoes
End Get
Set(value As ICollection(Of ToDo))
m_ToDoes = Value
End Set
End Property
Private m_ToDoes As ICollection(Of ToDo)
' FirstName & LastName will be stored in a different table called MyUserInfo
Public Overridable Property MyUserInfo() As MyUserInfo
Get
Return m_MyUserInfo
End Get
Set(value As MyUserInfo)
m_MyUserInfo = Value
End Set
End Property
Private m_MyUserInfo As MyUserInfo
End Class
Public Class MyUserInfo
Public Property Id() As Integer
Get
Return m_Id
End Get
Set(value As Integer)
m_Id = Value
End Set
End Property
Private m_Id As Integer
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(value As String)
m_FirstName = Value
End Set
End Property
Private m_FirstName As String
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(value As String)
m_LastName = Value
End Set
End Property
Private m_LastName As String
End Class
Public Class ToDo
Public Property Id() As Integer
Get
Return m_Id
End Get
Set(value As Integer)
m_Id = Value
End Set
End Property
Private m_Id As Integer
Public Property Description() As String
Get
Return m_Description
End Get
Set(value As String)
m_Description = Value
End Set
End Property
Private m_Description As String
Public Property IsDone() As Boolean
Get
Return m_IsDone
End Get
Set(value As Boolean)
m_IsDone = Value
End Set
End Property
Private m_IsDone As Boolean
Public Overridable Property User() As ApplicationUser
Get
Return m_User
End Get
Set(value As ApplicationUser)
m_User = Value
End Set
End Property
Private m_User As ApplicationUser
End Class
Public Class MyDbContext
Inherits IdentityDbContext(Of ApplicationUser)
Public Sub New()
MyBase.New("conn1") 'DefaultConnection
End Sub
Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
MyBase.OnModelCreating(modelBuilder)
' Change the name of the table to be Users instead of AspNetUsers
modelBuilder.Entity(Of IdentityUser)().ToTable("Users")
modelBuilder.Entity(Of ApplicationUser)().ToTable("Users")
End Sub
Public Property ToDoes() As DbSet(Of ToDo)
Get
Return m_ToDoes
End Get
Set(value As DbSet(Of ToDo))
m_ToDoes = Value
End Set
End Property
Private m_ToDoes As DbSet(Of ToDo)
Public Property MyUserInfo() As DbSet(Of MyUserInfo)
Get
Return m_MyUserInfo
End Get
Set(value As DbSet(Of MyUserInfo))
m_MyUserInfo = Value
End Set
End Property
Private m_MyUserInfo As DbSet(Of MyUserInfo)
End Class
End Namespace
更新
根据评论,我还检查了here。
我真的不明白为什么这是一个角色问题,因为我正在重用一个已经可以很好地更新其他(非身份相关)表的连接字符串。我也不明白为什么错误是指master 表,因为我希望我不会尝试通过manager.Find 方法以任何方式连接到该表。
无论如何,我当前正在测试的用户已经分配给sysadmin 角色:
更新 2
好的,感谢 Jeremy 在下面的评论,我离我更近了一步……我需要将用户 IIS APPPOOL\.NET v4.5 添加到角色 sysadmin,因为那是 SQL Server 分析器中显示的连接用户(尽管我是不确定将此用户添加到此角色时的安全风险)。
无论如何,Find 方法不再抛出错误,并且使用 SQL Server 分析器我看到执行此代码时触发了 SQL 语句Dim user As User = manager.FindByName(Trim(Username.Text))
exec sp_executesql N'SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[ApplicationId] AS [ApplicationId],
[Extent1].[MobileAlias] AS [MobileAlias],
[Extent1].[IsAnonymous] AS [IsAnonymous],
[Extent1].[LastActivityDate] AS [LastActivityDate],
[Extent1].[MobilePIN] AS [MobilePIN],
[Extent1].[LoweredEmail] AS [LoweredEmail],
[Extent1].[LoweredUserName] AS [LoweredUserName],
[Extent1].[PasswordQuestion] AS [PasswordQuestion],
[Extent1].[PasswordAnswer] AS [PasswordAnswer],
[Extent1].[IsApproved] AS [IsApproved],
[Extent1].[IsLockedOut] AS [IsLockedOut],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[LastLoginDate] AS [LastLoginDate],
[Extent1].[LastPasswordChangedDate] AS [LastPasswordChangedDate],
[Extent1].[LastLockoutDate] AS [LastLockoutDate],
[Extent1].[FailedPasswordAttemptCount] AS [FailedPasswordAttemptCount],
[Extent1].[FailedPasswordAttemptWindowStart] AS [FailedPasswordAttemptWindowStart],
[Extent1].[FailedPasswordAnswerAttemptCount] AS [FailedPasswordAnswerAttemptCount],
[Extent1].[FailedPasswordAnswerAttemptWindowStart] AS [FailedPasswordAnswerAttemptWindowStart],
[Extent1].[Comment] AS [Comment],
[Extent1].[Email] AS [Email],
[Extent1].[EmailConfirmed] AS [EmailConfirmed],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
[Extent1].[PhoneNumber] AS [PhoneNumber],
[Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Extent1].[LockoutEnabled] AS [LockoutEnabled],
[Extent1].[AccessFailedCount] AS [AccessFailedCount],
[Extent1].[UserName] AS [UserName]
FROM [dbo].[AspNetUsers] AS [Extent1]
WHERE ((UPPER([Extent1].[UserName])) = (UPPER(@p__linq__0))) OR ((UPPER([Extent1].[UserName]) IS NULL) AND (UPPER(@p__linq__0) IS NULL))',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'flo@outlook.com'
奇怪的是:当我直接在 SQL Server Management Studio 中执行它时,我会返回一条记录,但在我的代码中,变量 user 是 Nothing....它会是什么?
【问题讨论】:
-
Stacktrace 表明 ASP.Net 身份/实体框架尝试创建数据库。这是你所期望的吗?换句话说 - 数据库“myapp”是否已经存在于您的 sql 服务器上?
-
这确实不是我所期望的。数据库 myapp 已经存在。就好像我这里传递的自定义数据库连接字符串被忽略了:
Public Sub New() MyBase.New("conn1") 'DefaultConnection End Sub -
错误是指主数据库,而不是主表。
-
@StingyJack:master 数据库在做什么?我该如何解决这个问题?
标签: asp.net sql-server vb.net asp.net-identity