【问题标题】:Asp.net core identity method FindByEmailAsync throws exceptionAsp.net 核心标识方法 FindByEmailAsync 抛出异常
【发布时间】:2021-04-30 01:48:08
【问题描述】:

我正在使用 .net core 5 和 identity core,我的项目运行良好,直到我开始在 FindByEmailAsync 方法中遇到错误

 identityUser = await userManager.FindByEmailAsync(userinfo.Email);

例外: 数据为空。不能对 Null 值调用此方法或属性。

这是微软代码的堆栈跟踪:

   at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
   at Microsoft.Data.SqlClient.SqlBuffer.get_String()
   at Microsoft.Data.SqlClient.SqlDataReader.GetString(Int32 i)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__14.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__15`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__15`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNetCore.Identity.UserManager`1.<FindByEmailAsync>d__116.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

请注意 userMangeruserinfouserinfo.Email 不为空。

【问题讨论】:

    标签: c# asp.net-core asp.net-identity asp.net-core-identity


    【解决方案1】:

    我发现了问题。我已将以下属性添加到 IdentityUser 用户:

       public class AppIdentityUser : IdentityUser<int>
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    

    对于我通过电子邮件选择的用户,它们具有空值。我将它们更改为可为空的字符串,问题就解决了(在应用迁移之后)。

            public string? FirstName { get; set; }
            public string? LastName { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2021-12-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多