我有一个场景,我需要访问构造函数控制器中的
User.Identity
声明。
我需要这个,因为声明包含我启动自定义数据库上下文(连接字符串)所需的信息
我怎样才能访问这个?我只会注入 DBContext,但根据用户,他们可能需要访问不同的数据库。
有更好的方法来思考这个问题吗?
[Authorize]
public class DefaultController : Controller
{
public DefaultController()
{
// this is NULL
var authenticatedUser = User.Identity.Name;
}
}
从 ASP.NET Core 2.1 版本开始,提供了
HttpContextAccessor
。为此,我们必须遵循以下步骤:
使用
ConfigureServices
的 Startup.cs
方法中的标准实现配置服务:
services.AddHttpContextAccessor();
在控制器上执行
IHttpContextAccessor
的依赖注入。使用 HttpContext
属性访问 User
[Authorize]
public class DefaultController : Controller
{
public DefaultController(IHttpContextAccessor contextAccessor)
{
// Here HttpContext is not Null :)
var authenticatedUser = contextAccessor.HttpContext.User.Identity.Name;
}
}
来自 Rabea AlTaradeh 上面的链接:
Startup.cs
//services section
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
[Authorize]
public class HomeController : Controller
{
#region DI
private string UserEmail;
private readonly IHttpContextAccessor _httpContextAccessor;
public HomeController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
UserEmail = _httpContextAccessor.HttpContext.User.Claims
.FirstOrDefault(c => c.Type == "preferred_username")?.Value;
}
#endregion DI
public IActionResult Index()
{
ViewBag.UserEmail = UserEmail;
return View();
}
}
要访问 .Net core 中的 User.Identity Claims,您需要使用 HttpContext。这是我的以下实现
首先注册
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
然后在Service或者Controller类中注册
private readonly IHttpContextAccessor _httpContextAccessor;
public UserService(
IHttpContextAccessor httpContextAccessor,
IUnitOfWork unitOfWork,
UserManager<User> userManager,
SignInManager<User> signInManager,
RoleManager<ApplicationRole> roleManager)
{
_httpContextAccessor = httpContextAccessor;
_unitOfWork = unitOfWork;
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
_currentUserGuid = _httpContextAccessor?.HttpContext?.User?.FindFirst(UserClaimsKey.Sub)?.Value;
_currentUserName = _httpContextAccessor?.HttpContext?.User?.Identity?.Name;
_currentUserEmail = _currentUserGuid == null ? "" : userManager.FindByIdAsync(_currentUserGuid)?.Result?.Email;
}
如有任何问题请告诉我
这是我用来获取索赔的代码:
int user_id = 0;
int Account_id = 0;
List<string> roles;
public TEMP_CON_Controller(DatabaseContext context,IHttpContextAccessor,httpContextAccessor)
{
_context = context;
if (httpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
{
var user = httpContextAccessor.HttpContext.User;
this.user_id = int.Parse(user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value);
IEnumerable<Claim> claims = user.Claims;
var accId = claims.Where(x => x.Type == "AccountId").Select(c => c.Value).SingleOrDefault();
if (!string.IsNullOrEmpty(accId))
this.Account_id = Int32.Parse(accId);
roles = claims.Where(x => x.Type == ClaimTypes.Role).Select(c => c.Value).ToList();
}
}
在 Asp.net Core v.2+ 中,您不必注入 HttpContext 之类的东西。像这样访问用户:
public IActionResult GetMe()
{
return Ok(User.Identity.Name);
}