Hello guys am currently building a Web App with ASP.Net Core 2.0. Am really stack have tried a couple of solutions but have not been able to achieve my Goal. The issue is, On the Register section I happen to have multiple SignUp Forms(12 of them each with different properties according to the user registering to the App).
The issue is figuring out away to configure all these forms to work and save users to the db. Also to configure a single Login section that validates(UserName, Password, Bool remember me) and handles logIn input from any registered user regardless of the Form they SignUp with.
Here is what have been able to come Up with so far:
Register.cshtml.cs
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using FarmMart.Data;
using FarmMart.Services;
using FarmMart.Models.RegisterForms;
using Microsoft.AspNetCore.Authorization;public class RegisterModel : PageModel
{private readonly SignInManager<ApplicationUser> _signInManager;private readonly UserManager<ApplicationUser> _userManager;private readonly ILogger<LoginModel> _logger;private readonly IEmailSender _emailSender;public RegisterModel(UserManager<ApplicationUser> userManager,SignInManager<ApplicationUser> signInManager,ILogger<LoginModel> logger,IEmailSender emailSender){_userManager = userManager;_signInManager = signInManager;_logger = logger;_emailSender = emailSender;}
[BindProperty]
Public RegisterFormsModel Register { get; set; }
I created a RegisterForms Folder in the Models folder Which holds all the classes(11 Departments Classes and 1 Lecturer Class), where each class form has unique properties (username, Password, Emali Course etc).
Public class RegisterFormsModel{internal static string Password = null;internal string UserName = null;internal string Email = null;public Lecturer Lecturer { get; set; }public Department1 Department1 { get; set; }public Department2 Department2 { get; set; }public Department3 Department3 { get; set; }public Department4 Department4 { get; set; }public Department5 Department5 { get; set; }public Department6 Department6 { get; set; }public Department7 Department7 { get; set; }public Department8 Department8 { get; set; }public Department9 Department9 { get; set; }public Department10 Department10 { get; set; }public Department11 Department11 { get; set;
}public void OnGet(string returnUrl = null){ReturnUrl = returnUrl;}public async Task<IActionResult> OnPostAsync(string returnUrl = null){ReturnUrl = returnUrl;if (ModelState.IsValid){var user = new ApplicationUser{UserName = Register.UserName, Email = Register.Email,};var result = await _userManager.CreateAsync(user, RegisterFormsModel.Password);if (result.Succeeded){_logger.LogInformation("User created a new account with password.");var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);await _emailSender.SendEmailConfirmationAsync(Register.Email, callbackUrl);await _signInManager.SignInAsync(user, isPersistent: false);return LocalRedirect(Url.GetLocalUrl(returnUrl));}foreach (var error in result.Errors){ModelState.AddModelError(string.Empty, error.Description);}}// If we got this far, something failed, redisplay formreturn Page();}
Please anyone with an idea or best practice advice on how to go about it, I will really appreciate it .Thank you