我有一个.Net Core2 WebAPI控制器,需要在它的构造函数或某个路由中检索当前用户Id。
[Route("api/[controller]")]
public class ConfigController : Controller
{
private readonly IConfiguration _configuration;
public ConfigController(IConfiguration iConfig)
{
_configuration = iConfig;
}
[HttpGet("[action]")]
public AppSettings GetAppSettings()
{
var appSettings = new AppSettings
{
//Other settings
CurrentUser = WindowsIdentity.GetCurrent().Name
};
return appSettings;
}
}上面的WindowsIdentity.GetCurrent().Name不会给我需要的东西。我想我需要一个与.Net框架的System.Web.HttpContext.Current.User.Identity.Name等效的an
有什么想法吗?请注意,这是一款.Net Core2.0 WebAPI,请不要为常规.net控制器提供解决方案。
发布于 2019-01-03 18:13:07
ControllerBase.User将持有请求的当前已验证用户的原则,并且仅在执行操作的作用域中可用,而不在构造函数中可用。
[HttpGet("[action]")]
public AppSettings GetAppSettings() {
var user = this.User;
var appSettings = new AppSettings {
//Other settings
CurrentUser = user.Identity.Name
};
return appSettings;
}https://stackoverflow.com/questions/54019495
复制相似问题