2016-09-17 5 views
3

ASP.NET Identity 3.0의 컨트롤러에서 응용 프로그램 사용자를 얻으려면 googleing을 사용하고 있지만 결과는 5 이하입니다.컨트롤러에서 ApplicationUar 가져 오기

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

코드에 문제가 있습니까? 오류가 있었습니까? –

+0

Identity에 'GetUserId'에 대한 정의가없고 최상단 확장 메서드가 포함되어 있지 않습니다. ovreload 'PrincipalExtensions.GetUserId (ClaimsPrincipal)'에는 'ClaimsPrincipal'유형의 수신기가 필요합니다. – StuiterSlurf

답변

5

조의 대답은 잘 작동하지만, 간단한 방법도있다주의 할 것이다, 그 직접 ClaimsPrincipal 인스턴스를 받아 GetUserId를 호출 내부 :

var user = await _userManager.GetUserAsync(User); 
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(); 
} 

the way to get the userid changed back in RC2

관련 문제