我正在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());
}
}
}
答案 0 :(得分:6)
Joe的答案可以正常使用,但请注意,还有一个更简单的方法,直接采用import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
for(int i=0;i<size;i++){
for(int k=1;k<size-i;k++){
System.out.print(" ");
}
for(int j=size;j>=size-i;j--){
System.out.print("#");
}
System.out.println();
}
}
}
实例并在内部调用ClaimsPrincipal
:
GetUserId
答案 1 :(得分:2)
你应该改变你的代码:
public async Task<IActionResult> Index()
{
var userId = _userManager.GetUserId(User);
ApplicationUser currentUser = await _userManager.FindByIdAsync(userId);
if (currentUser.PropBool)
return View(currentUser);
return View();
}