Display Link based on User Role in ASP.Net MVC

on Apr 20, 2019 08:55 PM

Currently when I create a new user and the default Register() logs in the user My _layout displays what links depending on @if (User.IsInRole("User")).

Right now when I create the user the          

@if (Request.IsAuthenticated) displays the correct links.

Next it checks the Role and depending on the role it displays what it needs.

Currently no matter the role the user gets whatever the else {} displays.

If I log the user out and then log the user back in Then all the CORRECT layout links are shown. Any idea how I can fix this or why this is?

Here is my register its pretty much the default one…

        public async Task<ActionResult> Register(RegisterViewModel model)
            if (ModelState.IsValid)
                var user = new ApplicationUser
                    UserName = model.UserName,
                    Email = model.Email,
                    Name = model.Name,
                    PhoneNumber = model.PhoneNumber,
                    Birthday = Convert.ToDateTime(model.Birthday),
                    DateCreated = DateTime.Now,
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
                    TempData["Success"] = "User Created Successfully";

                    return RedirectToAction("Index", "Home");


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Apr 30, 2019 09:19 PM

This is all i needed to do to solve this was to move something in front of the login authencated part

if (result.Succeeded)
    // Assign Roles to User HERE *Adding here displays correct layout*
    await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);