Stu*_*urf 3 c# identity entity-framework-core asp.net-core
我正在google以获取ASP.NET Identity 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Joe的答案可以正常工作,但请注意,还有一个更简单的方法,它直接接受一个ClaimsPrincipal
实例并在GetUserId
内部调用:
var user = await _userManager.GetUserAsync(User);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1425 次 |
最近记录: |