[英]ViewComponent does not recognize AspNetCore.Identity "User"
I'm trying to create a ViewComponent for a series of checkboxes based on this article here:我正在尝试根据此处的这篇文章为一系列复选框创建一个 ViewComponent:
https://learn.microsoft.com/en-us/as.net/core/mvc/views/view-components?view=as.netcore-2.2 https://learn.microsoft.com/en-us/as.net/core/mvc/views/view-components?view=as.netcore-2.2
And this SO answer here: https://stackoverflow.com/a/42853705/2300177这个 SO 在这里回答: https://stackoverflow.com/a/42853705/2300177
Seems like a perfect case scenario for what I'm trying to do.对于我正在尝试做的事情来说,这似乎是一个完美的案例场景。 However, I've created my ViewComponent to include Identity
UserManager
to get the userId
:但是,我创建了我的 ViewComponent 以包含 Identity
UserManager
以获取userId
:
using BlogPlayground.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace BlogPlayground.ViewComponents
{
public class LatestArticlesViewComponent : ViewComponent
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityUser> _userManager;
public LatestArticlesViewComponent(ApplicationDbContext context, UserManager<IdentityUser> userManager)
{
_context = context;
_userManager = userManager;
}
public async Task<IViewComponentResult> InvokeAsync()
{
string userId = _userManager.GetUserId(User);
var lastArticles = await _context.Article
.OrderByDescending(a => a.CreatedDate)
.Take(howMany)
.ToListAsync();
return View(lastArticles);
}
}
}
The problem is that when I call this line:问题是当我调用此行时:
string userId = _userManager.GetUserId(User);
I'm getting an error for User
that reads:我收到一条
User
错误,内容如下:
cannot convert from 'System.Security.Principal.IPrincipal' to 'System.Security.Claims.ClaimsPrincipal'
无法从“System.Security.Principal.IPrincipal”转换为“System.Security.Claims.ClaimsPrincipal”
This exact code works in all my controllers and I have no idea where the User
even comes from.这个确切的代码适用于我所有的控制器,我什至不知道
User
来自哪里。 Seems like magic, but I'm guessing somewhere inherent inside Identity?看起来很神奇,但我猜是 Identity 内在的某个地方? Or did I just miss something included in my controllers?
还是我只是错过了控制器中包含的某些内容? (I searched "User" in my controllers and can't link that to anything.)
(我在我的控制器中搜索了“用户”,但无法将其链接到任何东西。)
I'm using the Pomelo MySQL nuget package if that makes a difference?我正在使用 Pomelo MySQL nuget package 如果这有什么不同? I wouldn't think so.
我不这么认为。
Anyone know what I'm missing and how to eliminate this error?任何人都知道我错过了什么以及如何消除这个错误?
Update: Seems it will work with this (at least it gets rid of the error):更新:似乎它可以解决这个问题(至少它摆脱了错误):
string userId = _userManager.GetUserId(Request.HttpContext.User);
Is that the same thing?那是一回事吗? Is there a using statement I'm missing?
我缺少 using 语句吗? Visual studio (2017) usually clues me in to what I'm missing.
Visual studio (2017) 通常会提示我所缺少的内容。
Please make sure you configured services.AddDefaultIdentity
and enabled app.UseAuthentication
Identity
in your Startup
. 请确保在
Startup
配置了services.AddDefaultIdentity
并启用了app.UseAuthentication
Identity
。
This is needed to make it available to the app through dependency injection
, so you can get it loaded in your LatestArticlesViewComponent
. 这需要通过
dependency injection
使其可用于应用程序,因此您可以将它加载到您的LatestArticlesViewComponent
。
See the details here: Introduction to Identity on ASP.NET Core 请参阅此处的详细信息: ASP.NET Core上的Identity简介
You should use the UserClaimsPrincipal
property of ViewComponent
instead of the User
property, so您应该使用
ViewComponent
的UserClaimsPrincipal
属性而不是User
属性,因此
string userId = _userManager.GetUserId(UserClaimsPrincipal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.