Search code examples
angularvisual-studioasp.net-coreentity-framework-coreabp-framework

How to override abp framework Register page and add some extra fields


I am using angular and Entityframework with separate identity Server template. I have override login page design from https://docs.abp.io/en/abp/latest/Customizing-Application-Modules-Extending-Entities and for Register page I have tried the following code but unable to set extra properties to IdentityUser.

public class RegisterModel : Volo.Abp.Account.Web.Pages.Account.RegisterModel
{
    //private readonly IMyAccountAppService myAccountAppService;

    public RegisterModel(IAccountAppService accountAppService):base(accountAppService)
    {
        //this.myAccountAppService = myAccountAppService;
    }

    [BindProperty]
    public new PostInput Input { get; set; }

    public override async Task<IActionResult> OnPostAsync()
    {
        try
        {
            Input.EmailAddress= "testuser@test.com";
            if (ModelState.IsValid)
            {
                await CheckSelfRegistrationAsync();

                if (IsExternalLogin)
                {
                    var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
                    if (externalLoginInfo == null)
                    {
                        Logger.LogWarning("External login info is not available");
                        return RedirectToPage("./Login");
                    }

                    await RegisterExternalUserAsync(externalLoginInfo, Input.UserName);
                }
                else
                {
                    await RegisterLocalUserAsync();
                }

                return Redirect(ReturnUrl ?? "~/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow! 
            }
            return Page();
        }
        catch (BusinessException e)
        {
            Alerts.Danger(e.Message);
            return Page();
        }
    }

    protected override async Task RegisterLocalUserAsync()
    {
        ValidateModel();

        //var userDto = await myAccountAppService.UserRegisterAsync(
        //    new MyRegisterDto
        //    {
        //        AppName = "MVC",
        //        Password = Input.Password,
        //        UserName = Input.UserName,
        //        Age=Input.Age,
        //        Name=Input.Name,
        //        Sex=Input.Sex,
        //        Terms=Input.Terms
        //    }
        //);
        //await CheckSelfRegistrationAsync();
        var user = new Volo.Abp.Identity.IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id);
        
        
        (await UserManager.CreateAsync(user, Input.Password)).CheckErrors();

        //await UserManager.SetEmailAsync(user, input.EmailAddress);
        await UserManager.AddDefaultRolesAsync(user);
        var user1 = await UserManager.GetByIdAsync(user.Id);
        await SignInManager.SignInAsync(user1, isPersistent: true);
    }

    public new class PostInput
    {
        [Required]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
        public string Name { get; set; }

        [Required]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
        public string Age { get; set; }
        [Required]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
        public string Sex { get; set; }
        [Required]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
        public string UserName { get; set; }

        [Required]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))]
        [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{6,}$",
     ErrorMessage = "Passwords must have at least one lowercase , Passwords must have at least one uppercase.")]
        [DataType(DataType.Password)]
        [DisableAuditing]
        public string Password { get; set; }

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

        [Display(Name = "Terms and Conditions")]
        [Range(typeof(bool), "true", "true", ErrorMessage = "Check Terms and Conditions.")]
        public bool Terms { get; set; }

        [Required]
        [EmailAddress]
        [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxEmailLength))]
        public string EmailAddress { get; set; } = "testuser@test.com";
    }
}

here is my project solution

[[1]: https://i.sstatic.net/VYCTV.png][1]


Solution

  • This article explains how you can add new properties to the AppUser class. You can follow the article to extend the IdentityUser class.