Question

1) I'm trying to allow the user to change their UserName (email) etc. in a ASP.MVC/Identity app. Using FluentValidation I'd like to check that no other user is using the new email.

In a controller I'd use User.Identity.GetUserId() but this isn't available when building Validation rules. How can I retreive the currently authenticated 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) What is the best way to store such changes. Right now I'm using 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) In my header I display the currently logged in user using User.Identity.GetUserName() but after I've changed the UserName this text isn't updated. I need to logout and login again to update this. Is there a way to get ASP to update the current user?

Was it helpful?

Solution

1) I'm trying to allow the user to change their UserName (email) etc. in a ASP.MVC/Identity app

Asp.net identity has a class called UserManager. This class exposes many function including Update which will automatically update the user's data in UserStore.

Using FluentValidation I'd like to check that no other user is using the new email.

If you use Create function of UserManager class for creating new user then this function automatically take care about the duplicate username entry validation. In case of invalid Or duplicate entry, Create function return well descriptive error messages which you can show to your users.

2) What is the best way to store such changes. Right now I'm using AutoMapper:

I don't understand the code you mentioned in your 2 point. The only purpose of Automapper is to mapped one object to another. In that code

  • you fetch the user from the database
  • create a mapping configuration (You should do this thing only once, no need to create mapping configuration again and again. So the best place for such type of things is global.asax file)
  • Then you mapped your viewmodel object to domain model object.

But this thing never put any change in the current context, so context.SaveChanges() have no effect.

And i think this is the reason why your User.Identity.GetUserName() is not updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top