asp.net-mvc – 在布局视图中获取当前的ApplicationUser
发布时间:2020-08-03 06:06:05 所属栏目: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 – IIS 404自定义错误不能按预期工作
- asp.net通过Ajax UpdatePanel回传后滚动条位置变更解决方法
- asp.net – .NET Web API 2 OWIN承载令牌认证
- NHibernate中对同一个对象的Lazyload要设置一致
- asp.net-mvc – 输出在剃刀通过变量?
- 详解Asp.net Core 使用Redis存储Session
- asp.net-mvc-3 – 使用@ Html.Raw有风险吗?
- ASP.NET MVC删除操作方法中的查询字符串
- asp.net-mvc-4 – MVC4不要在重定向上使用主布局
- asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javasc
推荐文章
站长推荐
- asp.net – VirtualPath位于当前应用程序根目录之
- asp.net – Azure可以运行WPF吗?
- webforms – ASP.Net Core 1.0是否支持WebForm项
- asp.net-mvc-4 – MVC4不要在重定向上使用主布局
- ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实
- asp.net – 我应该在哪里放置scriptmanager
- asp.net-mvc – ASP.NET MVC控制器的[Authorize]
- asp.net – MS Chart for .NET预定义调色板颜色列
- asp.net-mvc – ASP.NET MVC 3列表到IEnumerable
- 在asp.net中将用户变量存储在数据库与会话中
热点阅读
