我正在谷歌获取ASP.NET身份3.0中的控制器中的应用程序用户,但我只找到5和更低的结果。获取应用程序用户在控制器中
我想获得ApplicationUser
属性,但我根本得不到ApplicationUser
。我想这很容易,因为我是唯一有这个问题的人。
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNet.Identity;
using System.Security.Claims;
namespace ApplicationUserFMLTest.Controllers
{
public class CustomClassController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private ApplicationDbContext _context;
public CandidatesController(ApplicationDbContext context, UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{
_context = context;
_userManager = userManager;
_signInManager = signInManager;
}
// GET: CustomClass
public async Task<IActionResult> Index()
{
ApplicationUser currentUser = _userManager.FindByIdAsync(User.Identity.GetUserId()).Result;
if (currentUser.PropBool)
return View(currentUser);
return View();
}
// ManageController function
private async Task<ApplicationUser> GetCurrentUserAsync()
{
return await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
}
}
}
什么是你的代码有问题吗?你有错误吗? –
它表示身份不包含'GetUserId'的定义,并且最佳扩展方法ovreload'PrincipalExtensions.GetUserId(ClaimsPrincipal)'需要类型为'ClaimsPrincipal'的接收者, – StuiterSlurf