Два вопроса о MVC и идентичности
Я очень новичок в идентичности и MVC, я пытаюсь создать приложение MVC как один из моих первых проектов.
Я был в состоянии следоватьнесколько уроков и успешно добавили дополнительные свойства в мой класс 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
}
Мои вопросы:
Я вижу, где для электронной почты установлено, что требуется уникальный Email в 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 (), есть ли способ ссылаться на одно из моих пользовательских свойств, таких как FirstName, или ручкой?
@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>
}