Why is WorkContext.CurrentUser always null?
I am working on the Orhcard module and I need to get the current user id. I tried everything I could google, but nothing worked. CurrentUser is always null. Here are the last things I've tried
public MyWidgetDriver(IWidgetService widgetService,
IAuthenticationService authenticationService,RequestContext requestContext,
IWorkContextAccessor workContextAccessor, IOrchardServices orchSvc)
{
var a = authenticationService.GetAuthenticatedUser();
var b = orchSvc.WorkContext.CurrentUser;
var c = workContextAccessor.GetContext().CurrentUser;
}
a, b and c are all null values. I can get the username from HttpContext.User, but I need an ID. the user is logged in. I have tried various accounts and account types. I can see that other parts that work with the user are working and can show the username via the WorkContext.
What am I missing?
+3
source to share
1 answer
It looks like you are trying to access it from the constructor. You need to actually implement these services before using them. So that...
private readonly IOrchardServices _orchardServices;
public MyWidgetDriver(IOrchardServices orchardServices) {
_orchardServices = orchardServices;
}
public DriverResult Display(...) {
var b = _orchardServices.WorkContext.CurrentUser;
var id = b.Id;
}
+4
source to share