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

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

$
0
0

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/


Viewing all articles
Browse latest Browse all 9386

Trending Articles