【问题标题】:Identity Server 4 loggs (Invalid object name 'DeviceCodes') exceptionIdentity Server 4 日志(无效的对象名称“DeviceCodes”)异常
【发布时间】:2020-07-18 12:39:42
【问题描述】:

我们正在使用 Identity Server 4 进行身份验证。在将我们的解决方案从 .Net Core 2.0 升级到 .Net Core 3.1 后,我们开始注意到当客户端重定向到我们的身份验证 URL 时会记录一个新异常,这并没有影响该过程,最终用户似乎一切正常。例外是:

An exception occurred while iterating over the results of a query for context type 'IdentityServer4.EntityFramework.DbContexts.PersistedGrantDbContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'DeviceCodes'.
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__164_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
ClientConnectionId:2b07bb46-18fe-4553-a79f-89b0186556d4
Error Number:208,State:1,Class:16

导致此异常的查询:

Failed executing DbCommand (2ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT [d].[UserCode], [d].[ClientId], [d].[CreationTime], [d].[Data], [d].[DeviceCode], [d].[Expiration], [d].[SubjectId]
FROM [DeviceCodes] AS [d]
WHERE [d].[Expiration] < GETUTCDATE()
ORDER BY [d].[DeviceCode]

我们的上下文中没有此表“DeviceCodes”,请提供任何建议!

【问题讨论】:

    标签: authentication asp.net-core exception client-server identityserver4


    【解决方案1】:

    您缺少 DeviceCodes 表,您将通过从包管理器控制台窗口添加迁移获得(现在您已升级),如下所示:

    add-migration Core3Upgrade -Context PersistedGrantDbContext
    

    (记得根据你的设置在VS中设置正确的Startup项目,在Package Manager Console中设置相应的Default项目,否则会失败。)

    然后……

    update-database -Context PersistedGrantDbContext
    

    (或运行任何初始化代码,您可能会使用这些代码来为您的身份验证数据库播种/应用迁移)

    如果成功,应该执行以下 SQL:

      Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE [DeviceCodes] (
          [UserCode] nvarchar(200) NOT NULL,
          [DeviceCode] nvarchar(200) NOT NULL,
          [SubjectId] nvarchar(200) NULL,
          [ClientId] nvarchar(200) NOT NULL,
          [CreationTime] datetime2 NOT NULL,
          [Expiration] datetime2 NOT NULL,
          [Data] nvarchar(max) NOT NULL,
          CONSTRAINT [PK_DeviceCodes] PRIMARY KEY ([UserCode])
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2018-04-22
      • 2018-08-08
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      相关资源
      最近更新 更多