asp.net-mvc – 在布局视图中获取当前的ApplicationUser
发布时间:2020-05-22 12:00:31 所属栏目:asp.Net 来源:互联网
导读:我正在使用MVC5,使用自定义属性创建了ApplicationUser:IdentityUser.现在我想在layout.cshtml中获取一个自定义属性(Avatar),以在不同的布局(标题,侧边栏)视图中显示登录的用户图像.我怎么做? public class ApplicationUser : IdentityUser{ public string A
|
我正在使用MVC5,使用自定义属性创建了ApplicationUser:IdentityUser.现在我想在layout.cshtml中获取一个自定义属性(Avatar),以在不同的布局(标题,侧边栏)视图中显示登录的用户图像.我怎么做? 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在我的视图中获取登录的用户名.我也想要用户形象. 我怎么能得到它? 解决方法您可以添加头像属性作为IdentityClaimpublic 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;
}
}
在剃刀内部视图中,您可以像这样访问它 @{
var avatar = ((ClaimsIdentity)User.Identity).FindFirst("avatar");
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在哪里?Json.Encode或@Json.Decode方法在M
- 将Funscript添加到现有的ASP.NET MVC项目中
- asp.net-mvc – 在没有模型的情况下手动将验证添加到文本框
- 用ASP.NET MVC阻止ZmEu攻击
- asp.net – 使用json仅序列化对象的一部分
- asp.net-mvc – 通过Gitignore递归地包含Nuget DLL
- dependency-injection – 如何使用unity注入ApplicationUse
- asp.net – Azure上的联合身份验证
- asp.net – 是否可以在源代码中使用iframe和localhost地址?
- asp.net – ActiveDirectoryMembershipProvider“无法联系指
推荐文章
站长推荐
- asp.net – 登录后对Membership.GetAllUsers()的
- 什么是ASP.Net相当于PHP的回声?
- 深入理解Asp.Net中WebForm的生命周期
- asp.net-mvc-2 – 使用Html.LabelFor显示人类可读
- asp.net – 可以使用__doPostBack()吗?
- 如何在ASP.Net Web API中为特定控制器启用Window
- asp.net-mvc – 真实世界的ASP.NET MVC存储库
- asp.net-mvc – 在ASP.NET MVC中下载动态生成的文
- 任何使用ASP.Net AJAX和JQuery的缺点
- asp.net – 在web.config中将子文件夹重写为子域
热点阅读
