.Net Core IdentityServer4 Get Authenticated User

会有一股神秘感。 提交于 2019-12-05 09:49:35

This should work for you:

var user = (HttpContext.User.Identity as ClaimsIdentity);

And then the user object has what you need.

I figured out how to do this. Since, I am using a custom service which needs the HttpContext Injected into it I needed to register an accessor as injectable:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Then in my Authentication Manager I can Access my HttpContext

public class UserAuthenticationManager : IUserAuthenticationManager
{
    HttpContext _httpContext;

    public UserAuthenticationManager(IHttpContextAccessor httpContextAccessor)
    {
        this._httpContext = httpContextAccessor?.HttpContext;
    }
    public ClaimsIdentity GetClaimsIdentity()
    {
        return (this._httpContext.User.Identity as ClaimsIdentity);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!