【发布时间】:2016-12-15 06:26:09
【问题描述】:
在ASP.Net MVC 5 中,ApplicationUser 可以扩展为具有自定义属性。我对其进行了扩展,现在它有了一个名为DisplayName 的新属性:
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
public string ConfirmationToken { get; set; }
public string DisplayName { get; set; } //here it is!
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
我还在Visual Studio 中的Package-Manager Console 中使用Update-Database 命令更新了数据库表,以确保ApplicationUser class 和AspNetUsers 表之间的一致性。我已经确认DisplayName 的新列现在存在于AspNetUsers 表中。
现在,我想使用 DisplayName 而不是默认的 UserName 作为原始 _LoginPartial.cshtml View 中的文本。但正如你所见:
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
原来的_LoginPartialView.cshtml是用User.Identity.GetUserName()得到ApplicationUser的UserName。 User.Identity 有 GetUserId 和 Name、AuthenticationType 等...但是我如何让我的 DisplayName 显示?
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity