我试图从我的Web API中的控制器之外的Context中获取当前用户。我使用OpenIddict对用户进行身份验证,因此授权是由令牌进行的。
我在我的Startup.cs中注册了HttpContextAcessor作为单例:
function fish(i) {
this.f = $('#hlogo');
this.d = '-=1px';
this.animateFish = function() {
$(this.f).animate({
"left": this.d
}, 10, "linear", this.checkCollision.call(this));
}
this.checkCollision = function() {
this.animateFish();
//TypeError: this.animateFish is not a function
}
}
var f1 = new fish(1);
f1.animateFish();
之后,我在我的集线器中注入了HttpContexrAcessor,如下所示:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
问题是_context.User.Identity.Name始终为null。
有没有办法做到这一点?
答案 0 :(得分:2)
在使用.Net Core 2.0的情况下,需要在控制器外部获取用户ID,在Application Service中,我执行了以下操作:
在 Startup.cs 中,我在 ConfigureServices
下添加了此行services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
我创建了以下课程
public class UserResolverService
{
private readonly IHttpContextAccessor _httpContext;
private readonly UserManager<ApplicationUser> _userManager;
public UserResolverService(IHttpContextAccessor httpContext, UserManager<ApplicationUser> userManager)
{
_httpContext = httpContext;
_userManager = userManager;
}
public string GetUserId()
{
var userId = _userManager.GetUserId(_httpContext.HttpContext.User);
return userId;
}
}
在我的 Application Service 类中,我调用了 UserResolverService
public class MyApplicationService
{
private readonly ApplicationDbContext _context;
private readonly UserResolverService _userResolverService;
public MyApplicationService(ApplicationDbContext context, UserResolverService userResolverService)
{
_context = context;
_userResolverService = userResolverService;
}
public async Task Create(MyModel entity)
{
entity.CreatorUserId = _userResolverService.GetUserId();
_context.MyModel.Add(entity);
await _context.SaveChangesAsync();
}
}
此外,如果您不想使用 UserManager ,则可以简单地使 UserResolverService 类如下:
public class UserResolverService
{
private readonly IHttpContextAccessor _httpContext;
public UserResolverService(IHttpContextAccessor httpContext)
{
_httpContext = httpContext;
}
public string GetUserId()
{
var user = _httpContext?.HttpContext?.User as ClaimsPrincipal;
var userId = user.Claims.ElementAt(0).Value;
return userId;
}
}
答案 1 :(得分:1)
试试这个:https://msdn.microsoft.com/pt-br/library/system.threading.thread.currentprincipal(v=vs.110).aspx
public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
HttpResponseMessage response;
try
{
if (activity.Type == ActivityTypes.Message)
{
//do some stuff
await Conversation.SendAsync(activity, () => new RootDialog());
}
else
{
HandleSystemMessage(activity);
}
response = this.Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
//do some catching
}
return response;
}
答案 2 :(得分:-1)
尝试从启动中删除此行:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
我相信框架已经为你注册了。
答案 3 :(得分:-2)
我遇到了同样的问题。对我来说,解决方案正在等待完成请求。 我发布了一个命令,并没有等待结果。 E.g。
PostSomething() {
Task.Run(() => PostCommandAndDontWait());
}
现在我的httpContext用户没有我期望的声明。 当我等待任务完成时(删除Task.Run,.Wait()。Result()等。我确实让用户拥有例外索赔。
答案 4 :(得分:-4)
试试这个:
HttpContext.Current.User.Identity.Name