Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Accessing ApplicationUser Property in the Login.cshtml.cs page

$
0
0

I'm developing web application using ASP.Net MVC Core. I used "Individual user authentication" so the identity table, Register and Login pages is generated by default.

I extended the identity table with enum column called "AccountStatus" that has Four values as following:

publicclassApplicationUser:IdentityUser{publicintStatusId{get;set;}publicstringStatusName{get;set;}publicAccountStatusIdAccountStatusId{get;set;}publicAccountStatusAccountStatus{get;set;}}publicenumAccountStatusId:int{Active=0,InActive=1,Expired=2,Pending=3}publicclassAccountStatus{publicAccountStatusIdAccountStatusId{get;set;}publicstringStatus{get;set;}publicList<ApplicationUser>Statuses{get;set;}}

I did the migration for the above code and I created 2 roles (Center and Admin).

publicclass SD{publicconststringAdminUser="Admin";publicconststringCenterUser="Center";}

so , I want in the Login.cshtml.cs below to check if the "AccountStatusId = AccountStatusId.Pending" then prevent the user from successful login and display a message "Waits for Approval".

But I'm not sure how can i access the field "AccountStatusId" which is in "ApplicationUser" model and check this field if "Pending" in Login.cshtml.cs page. Any help is appreciated.

Login.cshtml.cs

namespace RegisterTest.Areas.Identity.Pages.Account{[AllowAnonymous]publicclassLoginModel:PageModel{privatereadonlyUserManager<IdentityUser> _userManager;privatereadonlySignInManager<IdentityUser> _signInManager;privatereadonlyILogger<LoginModel> _logger;publicLoginModel(SignInManager<IdentityUser> signInManager,ILogger<LoginModel> logger,UserManager<IdentityUser> userManager){
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;}[BindProperty]publicInputModelInput{get;set;}publicIList<AuthenticationScheme>ExternalLogins{get;set;}publicstringReturnUrl{get;set;}[TempData]publicstringErrorMessage{get;set;}publicclassInputModel{[Required][EmailAddress]publicstringEmail{get;set;}[Required][DataType(DataType.Password)]publicstringPassword{get;set;}[Display(Name="Remember me?")]publicboolRememberMe{get;set;}}publicasyncTaskOnGetAsync(string returnUrl =null){if(!string.IsNullOrEmpty(ErrorMessage)){ModelState.AddModelError(string.Empty,ErrorMessage);}
                returnUrl = returnUrl ??Url.Content("~/");// Clear the existing external cookie to ensure a clean login processawaitHttpContext.SignOutAsync(IdentityConstants.ExternalScheme);ExternalLogins=(await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();ReturnUrl= returnUrl;}publicasyncTask<IActionResult>OnPostAsync(string returnUrl =null){
                returnUrl = returnUrl ??Url.Content("~/");if(ModelState.IsValid){// This doesn't count login failures towards account lockout// To enable password failures to trigger account lockout, set lockoutOnFailure: truevar result =await _signInManager.PasswordSignInAsync(Input.Email,Input.Password,Input.RememberMe, lockoutOnFailure:false);if(result.Succeeded){
                        _logger.LogInformation("User logged in.");returnLocalRedirect(returnUrl);}if(result.RequiresTwoFactor){returnRedirectToPage("./LoginWith2fa",new{ReturnUrl= returnUrl,RememberMe=Input.RememberMe});}if(result.IsLockedOut){
                        _logger.LogWarning("User account locked out.");returnRedirectToPage("./Lockout");}else{ModelState.AddModelError(string.Empty,"Invalid login attempt.");returnPage();}}// If we got this far, something failed, redisplay formreturnPage();}}}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>