【发布时间】:2015-05-31 05:39:15
【问题描述】:
我对身份和 MVC 非常陌生,我正在尝试创建一个 MVC 应用程序作为我的第一个项目之一。
我已经能够关注 few tutorials 并已成功向我的 ApplicationUser 中添加了其他属性:IdentityUser 类
public class ApplicationUser
: IdentityUser<string, ApplicationUserLogin,
ApplicationUserRole, ApplicationUserClaim>
{
[Required]
[Display(Name = "UserName")]
[StringLength(50)]
public string Handle { get; set; }
[StringLength(100, ErrorMessage = "Your {0} can be at most {1} characters long.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[StringLength(100, ErrorMessage = "Your {0} can be at most {1} characters long.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "User Creation Date")]
public DateTime UserCreationDate { get; set; }
public ApplicationUser()
{
this.Id = Guid.NewGuid().ToString();
// Add any custom User properties/code here
}
我的问题是:
-
我看到电子邮件在 App_Start.IdentityConfig.cs 中设置为需要唯一电子邮件的位置,有没有办法将其设置为需要像 Handle 这样的唯一自定义属性?
var manager = new ApplicationUserManager(new UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context.Get<ApplicationDbContext>())); // Configure validation logic for usernames manager.UserValidator = new UserValidator<ApplicationUser>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; -
在 Views.Shared._LoginPartial.cshtml 的部分视图中,它显示了使用 User.Identity.GetUserName() 登录应用程序的人的用户名/电子邮件是否有一种方法可以引用我的自定义属性之一而不是这样作为名字还是句柄?
@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> @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> } } 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> }
【问题讨论】:
标签: c# razor asp.net-mvc-5 asp.net-identity-2