asp.net-mvc – 在布局视图中获取当前的ApplicationUser
发布时间:2020-08-02 23:44: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-core – 在asp.net vnext中使用Startup.cs中的Conf
- Jquery 组合form元素为json格式,asp.net反序列化
- 从Asp.Net MVC 6 API返回JSON错误
- asp.net – 我的HTML5的doctype是否必须在第一行?
- asp.net – 使用app_offline.htm使应用程序脱机,同时在更新
- asp.net-mvc-3 – MVC 3 WebGrid – 可以进行内联编辑吗?
- asp.net-mvc – MicrosoftMvcValidation.js VS jquery.vali
- asp.net-mvc – 在部署MVC 2 Web应用程序时要绝对检查的事项
- asp.net-mvc – asp.net MVC antiorgerytoken异常RedirectT
- asp.net-mvc – 在IIS 5.1上部署ASP.NET MVC(Windows XP)
推荐文章
站长推荐
- 如何为Asp.Net中的所有子文件夹注册HttpHandler?
- asp.net – 如何禁用.NET事件日志警告?
- 为什么asp.net会员资格有用户表和会员表?
- asp.net-mvc – FluentValidation Autofac Valid
- asp.net-mvc – 如何将XML作为POST传递给ASP MVC
- ADO.NET 2.0 Dataset和Datatable 新功能新特性
- C#,.Net自动生成大写字母编码
- asp.net – 渗透测试人员说.ASPXAUTH cookie是不
- kendo-ui – 剑道网格刷新(数据绑定两次)
- asp.net – 允许服务器/ usercontrol上的任何属性
热点阅读