0

I've run into an issue when working with Microsoft MVC 5 and Identities 3.0.0 rc1-final.

So I've modified the AppUser.cs model that extends IdentityUser to include a FirstName and LastName column. My next objective is to create a claim for the user that stores there FullName, a concatenate of Firstname and Lastname. I've successfully done this when seeding the database. I can also successfully update the database when a user modifies there profile by changing there names. When a user makes a change to there profile I'd like to update the "FullName" claim within the database claims table as well as the users cookie so that I can display there full name in the header of the page. The problem is I can't seem to persist the data into the table. I am able to update the claims within the User object but that's about it. I've researched and tried a dozen different things but there doesn't seem to be a solution to my problem, below is some of my code:

AppUser.cs Model

public class AppUser : IdentityUser {
    [StringLength(255)]
    public string FirstName { get; set; }

    [StringLength(255)]
    public string LastName { get; set; }
}

Helper Classes. This was derived from my research and came accross this post. I realize I'm missing part of his method, the last 2 lines, but I could never get past compiliation errors when calling the authentication manager.

    public static void AddUpdateClaim(this IPrincipal currentPrincipal, string key, string value) {
        var identity = currentPrincipal.Identity as ClaimsIdentity;
        if (identity == null)
            return;

        // check for existing claim and remove it
        var existingClaim = identity.FindFirst(key);
        if (existingClaim != null)
            identity.RemoveClaim(existingClaim);

        // add new claim
        identity.AddClaim(new Claim(key, value));
    }

ManageController, this is the post method for updating user profile:

    public async Task<IActionResult> Index(IndexViewModel model) {
        if (!ModelState.IsValid) {
            return View(ModelState);
        }

        var user = await base.GetCurrentUserAsync();
        if (user != null) {
            user.Email = model.Email;
            user.FirstName = model.FirstName;
            user.LastName = model.LastName;

            User.AddUpdateClaim("FullName", "Test User");
            var result = await _userManager.UpdateAsync(user);
            if (result.Succeeded) {
                return RedirectToAction(nameof(Index), new { Message = ManageMessageId.AccoutUpdated });
            } else {
                _logger.LogError(1, "Error updating user: {username}", user.UserName);
                base.AddErrors(result);

                return View(model);
            }
        }

        // If we got this far, something failed, redisplay form with errors.
        return View(model);
    }

Any help at this point would be greatly appreciated as I've beaten my head against this wall for a while now!

Community
  • 1
  • 1
Shawn Abshire
  • 70
  • 2
  • 12

1 Answers1

0

I figured this out a couple days ago... basically I over complicated it. :P

Shawn Abshire
  • 70
  • 2
  • 12