0
votes

Now I know how to extend the User class through Identity, now what I need it is to know how to get these items at the hearing. In the IdentityModels

namespace ExtendIdentity.Models

{ // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { //Aqui se agregan las propiedades que uno quiere extender para la clase Usuario(paso 1) public string Email { get; set; }

    public string Area { get; set; }

}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

}

and ` public class RegisterViewModel { [Required] [Display(Name = "User name")] public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }


    //Aqui se gragan las propiedades, en el Viewmodel para que la vista los genere(Paso 2)
    [DataType(DataType.EmailAddress,ErrorMessage = "Por favor,entre un email valido")]
    [Required]
    public string Email { get; set; }

    [Required]
    public string Area { get; set; }


}

and AcountController

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            //Se modifica la siguiente linea para que se guarde en la base de datos los elementos nuevos (paso 3)
            // Si existe algun error se debe habilitar las migraciones y migrar
            var user = new ApplicationUser() { UserName = model.UserName , Email = model.Email, Area = model.Area  };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                AddErrors(result);
            }
        }

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

I need to get the Area and Email at the hearing, lighter or water

1
What?... Please make your question a bit more clear.Jamie Rees
Claims is what you're probably needing kevin-junghans.blogspot.com/2013/12/…JamieD77

1 Answers

0
votes

You could get user manager object every where by use of Owin context manager. And by help of user manager you could get user object by ID:

//make sure you added this line in the using section
using Microsoft.AspNet.Identity.Owin

 string myCustomProperty = HttpContext.Current.GetOwinContext()
     .GetUserManager<ApplicationUserManager>()
     .FindById(HttpContext.Current.User.Identity.GetUserId()).CustomPropertyName;