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

Redirecting after Login

$
0
0

Hi there,

I'm trying to redirect a user to the backend if they're in a certain role. I've tried a few different redirect methods but I cant seem to get them right and I can't a decent tutorial for asp.net core. Can anyone give me a hint to what I'm doing wrong? I did have the redirecttolocal set, but that didnt seem to work either.

        public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            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(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
                if (result.Succeeded)
                {
                    // Resolve the user via their email
                    var user = await _userManager.FindByEmailAsync(model.Email);
                    // Get the roles for the user
                    var roles = await _userManager.GetRolesAsync(user);

                    foreach (string role in roles) {
                        if (role == "Super Admin" || role == "Admin" || role == "Editor" || role == "Content Creator")
                        {
                            return RedirectToRoute("Index","DangerZone");
                        }
                        else
                        {
                            _logger.LogInformation("User logged in.");
                            return RedirectToLocal(returnUrl);
                        }
                    }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



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