Emi*_*mil 9 asp.net razor asp.net-mvc-4 asp.net-identity
我已将ApplicationUser
Class 和Last名称添加到Class中.
public class ApplicationUser : IdentityUser
{
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;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还添加了信息 RegisterViewModel
我的应用程序成功创建了First和LastName
表,但是我无法获得在_LoginPartial
"User.Identity.Name"中显示的名字和姓氏
Edi*_* G. 10
在你的部分视图中 _LoginPartial.cshtml
添加代码:
@if (Request.IsAuthenticated)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
...
}
Run Code Online (Sandbox Code Playgroud)
ApplicationDbContext
=您的DBContext - >默认值: ApplicationDbContext
随着实例ApplicationUser (user)
,你可以得到First-
和LastName
-property
替换User.Identity.Name
为user.FirstName + " " + user.LastName
这样:
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Hello " + user.FirstName + " " + user.LastName + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9585 次 |
最近记录: |