【发布时间】:2014-04-03 09:02:19
【问题描述】:
1) 我正在尝试允许用户在 ASP.MVC/Identity 应用程序中更改其用户名(电子邮件)等。使用 FluentValidation 我想检查没有其他用户正在使用新电子邮件。
在控制器中我会使用 User.Identity.GetUserId() 但这在构建验证规则时不可用。如何检索当前已通过身份验证的 userId?
public class ManageUserViewModelValidator : AbstractValidator<ManageUserViewModel>
{
public ManageUserViewModelValidator()
{
RuleFor(e => e.UserName)
.NotEmpty()
.WithMessage("Måste ange din e-post adress.")
.EmailAddress()
.WithMessage("Ogiltig e-post adress.")
.Must((model, _) => UserMethods.IsUserNameAvailable(model.UserName, "userId???"))
.WithMessage("E-post adressen är upptagen.");
}
...
}
[FluentValidation.Attributes.Validator(typeof(ManageUserViewModelValidator))]
public class ManageUserViewModel
{
[Display(Name = "E-post*")]
public string UserName { get; set; }
...
}
2) 存储此类更改的最佳方式是什么。现在我正在使用 AutoMapper:
public static void Update(this DbContext context, ManageUserViewModel model, string userId)
{
User user = context.Users.FirstOrDefault(e => e.Id.Equals(userId));
Mapper.CreateMap<ManageUserViewModel, TessinUser>();
Mapper.Map(model, user);
context.SaveChanges();
}
3) 在我的标题中,我使用User.Identity.GetUserName() 显示当前登录的用户,但是在我更改了用户名之后,此文本没有更新。我需要注销并再次登录才能更新。有没有办法让 ASP 更新当前用户?
【问题讨论】:
标签: asp.net-mvc automapper asp.net-identity fluentvalidation