gpt4 book ai didi

asp.net-core - 从 asp.net 核心身份获取 applicationuser 的属性

转载 作者:行者123 更新时间:2023-12-01 13:42:07 24 4
gpt4 key购买 nike

我找到了这个链接:

How to get the current logged in user Id ASP.NET Core

但是答案都过时了!

这个 SO 帖子既不是解决方案,它只是一个样板解决方法:How should I access my ApplicationUser properties from within my MVC 6 Views?

在 mvc 操作中:base.User 不是 ApplicationUser 类型,也不是 base.User.Identity。两者都不能转换为 ApplicationUser。

那么如何在登录时访问我的自定义属性放在 applicationuser 对象中?

最佳答案

对于 Controller ,依赖于UserManager<ApplicationUser> .然后,您可以通过请求的 HttpContext 访问 ApplicationUser。我为我的项目写了一个扩展方法:

 public static class IdentityExt
{
public static Task<T> GetCurrentUser<T>(this UserManager<T> manager, HttpContext httpContext) where T: class{
return manager.GetUserAsync(httpContext.User);
}
}

返回当前用户,并允许您访问他们的所有属性。

这是在一个 Action 中:

public Task<IActionResult> Index(){
var user = await _userManager.GetCurrentUser(HttpContext);
}

关于asp.net-core - 从 asp.net 核心身份获取 applicationuser 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com