2016-09-21 2 views
0

I 일부 페이지에서 렌더링 사이드 바에 ViewComponent를 사용합니다. 현재 로그인 사용자가 필요합니다. 어떻게 ViewComponent에서 사용자를 얻을 수 있습니까? 물론 뷰에서 InvokeAsync 메서드로 매개 변수로 전달할 수 있지만 더 나은 방법을 찾고 있습니다.ViewComponent에서 현재 로그인 사용자를 얻는 방법 Asp.Net Core

public class SideBarViewComponent : ViewComponent 
{ 
    private readonly UserManager<User> _userManager; 

    public ProfileSideBarViewComponent(UserManager<User> userManager) 
    { 
     _userManager = userManager; 
    } 

    public async Task<IViewComponentResult> InvokeAsync() 
    { 
     //How access User 

     _userManager.GetUserId(User); 

     return View(); 
    } 
} 

답변

1

수 대신 사용자의 사용자 UserClaimsPrincipal

에서 :이처럼 그것을 얻을 수 있도록

var user = this.HttpContext.User; 
2

글쎄, 당신은, 현재 HttpContext에 액세스 할 수있는 당신은 get or set the Current User을 할 수 있습니다.

ViewComponent에서
public async Task<IViewComponentResult> InvokeAsync() 
{ 
    _userManager.GetUserId(Request.HttpContext.User); 

    return View(); 
} 
1

당신이 요청에 액세스 할 수 있습니다 여기에

은 일부 의사 코드입니다 ViewComponent :

public abstract class ViewComponent 
{ 
    public ClaimsPrincipal UserClaimsPrincipal { get; } 
    .... 
} 

컨트롤러 있음 :

[Controller] 
public abstract class ControllerBase 
{ 
    protected ControllerBase(); 

    public ClaimsPrincipal User { get; } 

    .... 
} 
관련 문제