gpt4 book ai didi

asp.net-mvc - 在布局 View 中获取当前 ApplicationUser

转载 作者:行者123 更新时间:2023-12-04 14:11:37 25 4
gpt4 key购买 nike

我正在使用 MVC5,创建 ApplicationUser : IdentityUser带有自定义属性。现在我想在 layout.cshtml 中获得一个自定义属性(Avatar),以在不同的布局(标题、侧边栏) View 中显示登录的用户图像。我怎么做?

public class ApplicationUser : IdentityUser
{
public string Avatar { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}

}

目前我正在使用 @User.Identity.Name在我的 View 中获取登录的用户名。我也想要用户形象。

我怎么能得到那个?

最佳答案

您可以将头像属性添加为 IdentityClaim

public class ApplicationUser : IdentityUser
{
public string Avatar { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim("avatar", this.Avatar));
return userIdentity;
}
}

在 Razor View 中,您可以像这样访问它
@{
var avatar = ((ClaimsIdentity)User.Identity).FindFirst("avatar");
}

关于asp.net-mvc - 在布局 View 中获取当前 ApplicationUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038947/

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