我正在研究一个身份项目,我现在正在尝试创建一个端点来获取用户配置文件。我希望每个配置文件都有一个配置文件图像。
我在名为User
的新模型类中添加了自定义字段,如下所示:
public class User : IdentityUser
{
public string? ProfileImage { get; set; }
}
我还添加了startup.cs
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
还有AppDbContext
public class AppDbContext : IdentityDbContext<User>
{
public AppDbContext([NotNull] DbContextOptions options) : base(options) {}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
我也有AutoMapper映射我的UserDto与User类。
然而,在我的AuthController中,我试图创建一个端点来获取用户Id,我得到一个错误,IdentityUser不包含ProfileImage的定义。
[HttpGet("getUser/{id}")]
public async Task<ActionResult> GetCurrentUser(string id)
{
var user = await userManager.FindByIdAsync(id);
var model = new UserDto()
{
Id = user.Id,
UserName = user.Email,
Email = user.Email,
ProfileImage = user.ProfileImage <--- error on this line
};
return Ok(model);
}
我错过了什么,我能做些什么来解决这个问题?
多谢!
这里的问题是,我把IdentityUser
在UserManager
和SignInManager
,而不是我应该使用我的User
类
I changed from this
private readonly UserManager<IdentityUser> userManager;
private readonly SignInManager<IdentityUser> signInManager;
public AuthController(UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager)
{
this.userManager = userManager;
this.signInManager = signInManager;
}
private readonly UserManager<User> userManager;
private readonly SignInManager<User> signInManager;
public AuthController(UserManager<User> userManager,
SignInManager<User> signInManager)
{
this.userManager = userManager;
this.signInManager = signInManager;
}