locked
How to segregate Admin and Member view after login and throughout the system usage until logout? RRS feed

  • Question

  • User-467044538 posted

    Hello Net Core mastas!

    I know this sound ridiculous for one to ask this simple question here. This is my first attempt on developing projects base on Net Core. I have no experience on Net Framework before.

    I'm using Identity features of Net Core where I can have Admin and Member in the system.

    What I am trying to achieve is to segregate view between Admin role and Member role, this what i'm trying to do currently. This code is generated using Identity scaffold.

    public async Task<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: true
                    var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User logged in.");
    
                        var user = await _signInManager.UserManager.FindByEmailAsync(Input.Email);
    
                        var roless = await _signInManager.UserManager.GetRolesAsync(user);
    
                        //ViewData["UserInRole"] = roless[0];
    
                        _logger.LogInformation("User is in role ===================================> " + roless[0]);
                        _logger.LogInformation("Return Url ===================================> " + returnUrl);
    
                        if (roless[0] == "Admin")
                        {
                            // Return url to Admin controller
                            return RedirectToAction("Index", "Admin");
                        }
                        else if (roless[0] == "Member")
                        {
                            // Return url to Member controller
                            return RedirectToAction("Index", "Member");
                        }
    
                        //return LocalRedirect(returnUrl);
                    }
                    if (result.RequiresTwoFactor)
                    {
                        return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
                    }
                    if (result.IsLockedOut)
                    {
                        _logger.LogWarning("User account locked out.");
                        return RedirectToPage("./Lockout");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                        return Page();
                    }
                }
    
                // If we got this far, something failed, redisplay form
                return Page();
            }

    Is this the correct approach? Or is there any proper approaches? 

    Thanks in advance! Cheers \m/

    Thursday, August 8, 2019 7:11 AM

Answers

  • User711641945 posted

    Hi Frank420,

    Your code is the correct approach.And before you login,please be sure that adding roles and adding role to user are successful.Then your code would work well.

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 9, 2019 5:44 AM

All replies

  • User711641945 posted

    Hi Frank420,

    Your code is the correct approach.And before you login,please be sure that adding roles and adding role to user are successful.Then your code would work well.

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 9, 2019 5:44 AM
  • User-467044538 posted

    Hi. Thanks for the confirmation reply. Really appreciate it :)

    Friday, August 16, 2019 8:06 AM