简体   繁体   English

ViewComponent 无法识别 AspNetCore.Identity“用户”

[英]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您应该使用ViewComponentUserClaimsPrincipal属性而不是User属性,因此

string userId = _userManager.GetUserId(UserClaimsPrincipal);

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

相关问题 AspNetCore.Identity RemoveFromRoleAsync 不会从用户中删除角色 - AspNetCore.Identity RemoveFromRoleAsync does not remove role from user AspNetCore.Identity 不适用于自定义用户/角色实现 - AspNetCore.Identity not working with custom User/Role implementation AspNetCore.Identity - 如何为新用户设置 LockoutEnabled = false - AspNetCore.Identity - How to set LockoutEnabled = false for a new user AspNetCore.Identity中的Google身份验证 - Google authentication in AspNetCore.Identity 将 AddPooledDbContextFactory 与 AspNetCore.Identity 一起使用 - Using AddPooledDbContextFactory with AspNetCore.Identity 覆盖AspNetCore.Identity表中的默认索引 - Override default indexes in AspNetCore.Identity tables AspNetCore.Identity LockoutOptions.AllowedForNewUsers属性 - AspNetCore.Identity LockoutOptions.AllowedForNewUsers Property 身份服务器 4 + AspNetCore.Identity:X 尝试后如何锁定? - identity server 4 + AspNetCore.Identity: how to lockout after X tries? ASP.NET核心 - 自定义AspNetCore.Identity实现不起作用 - ASP.NET Core - custom AspNetCore.Identity implementation not working 如何在 AspNetCore.Identity 上删除基本字段并添加自定义字段? - How to delete basic fields and add custom fields on AspNetCore.Identity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM