简体   繁体   English

GetUserAsync 在 Razor EF ASP.NET 核心返回 null 时已经登录?

[英]GetUserAsync in Razor EF ASP.NET Core return null when already sign in?

Why does GetUserAsync return null?为什么 GetUserAsync 返回 null?

var currentUser = await _userManager.GetUserAsync(HttpContext.User);

Error:错误:

An unhandled exception occurred while processing the request.处理请求时发生未处理的异常。 NullReferenceException: Object reference not set to an instance of an object. NullReferenceException:Object 引用未设置为 object 的实例。 lambda_method178(Closure ) InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression. lambda_method178(Closure) InvalidOperationException:尝试评估 LINQ 查询参数表达式时引发异常。 See the inner exception for more information.有关更多信息,请参阅内部异常。 To show additional information call 'DbContextOptionsBuilder.EnableSensitiveDataLogging'.要显示附加信息,请调用“DbContextOptionsBuilder.EnableSensitiveDataLogging”。 Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.GetValue(Expression expression, out string parameterName) System.InvalidOperationException: An exception was thrown while attempting to evaluate a LINQ query parameter expression. Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractingExpressionVisitor.GetValue(Expression expression, out string parameterName) System.InvalidOperationException:尝试评估 LINQ 查询参数表达式时引发异常。 See the inner exception for more information.有关更多信息,请参阅内部异常。 To show additional information call 'DbContextOptionsBuilder.EnableSensitiveDataLogging'.要显示附加信息,请调用“DbContextOptionsBuilder.EnableSensitiveDataLogging”。 System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:Object 引用未设置为 object 的实例。 at lambda_method178(Closure )在 lambda_method178(闭包)

In my case I was using different branches with different DB, and claims ID were different, so when I signed in and debugged one then I went to another branched and sign in automatically, that when the error started.就我而言,我使用具有不同数据库的不同分支,并且声明 ID 不同,因此当我登录并调试一个分支时,我转到另一个分支并自动登录,即错误开始时。 So Sign out and sign in again solve the problem, because claims where different that you put in GetUserAsync().因此,注销并再次登录可以解决问题,因为声明与您在 GetUserAsync() 中的声明不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 内核 razor 页值不能为 null - ASP.NET core razor page value cannot be null IFormFile 始终为空(带有 MVC/Razor 的 ASP.NET Core) - IFormFile always null (ASP.NET Core with MVC/Razor) 在 ASP.NET 核心 Razor 页面中填充下拉列表时获取 null 引用异常 - Getting null reference exception when populating dropdown list in ASP.NET Core Razor Pages 角色是null at Post request for ASP.NET Core Razor Pages - Role is null at Post request for ASP.NET Core Razor Pages ASP.NET Core with Razor Pages: 从 DropDownList 返回值 - ASP.NET Core with Razor Pages: Return value from DropDownList ASP.NET Core 2.1 Razor 页面返回带模型的页面 - ASP.NET Core 2.1 Razor page return Page with model 插入到 EF Core ASP.NET Core Razor Pages .NET6 中自动生成的 Bridge 表 - Insert to autogenerated Bridge table in EF Core ASP.NET Core Razor Pages .NET6 ASP.NET Core 2.0无法返回EF触摸对象 - ASP.NET Core 2.0 fails to return EF touched object 在ASP.NET Core中具有EF Core的Razor Pages-更新相关数据-使用c#的8之7 - Razor Pages with EF Core in ASP.NET Core - Update Related Data - 7 of 8 with c# 当我在 asp.net core mvc 中未经授权时,如何始终返回登录/登录视图? - How to always return login/sign in view when I'm unauthorized in asp.net core mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM