【发布时间】:2016-03-25 20:29:30
【问题描述】:
我是 C# 新手,目前正在创建一个 ASP.NET MVC 应用程序。我目前在尝试按照此处找到的说明操作时遇到上述错误:Extending ASP.NET Identity
我试图让我的代码在用户登录后显示他们的名字而不是他们的电子邮件地址。
这是我的 _LoginPartial.cshtml 代码
@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav navbar-nav navbar-right">
<li>
@{
var manager = new UserManager<ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
}
@Html.ActionLink("Hello " + currentUser.FName + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
这是我在 IdentityModels.cs 中找到的 ApplicationUser 类
public class ApplicationUser : IdentityUser
{
public string FName { get; set; }
public string LName { get; set; }
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;
}
}
任何建议将不胜感激
【问题讨论】:
-
您需要在视图中为您的 ApplicationUser 类设置 using 指令,否则该类在视图中不可见
-
语法是什么?我试过了:@model ContactManager.Models.ApplicationUser 但没用
-
您的视图中已经有一个“正在使用”。语法相同,只是命名空间和类不同
标签: c# asp.net asp.net-mvc