How to inform (give refresh) the Asp.Net MVC application that a new user just logged in that already has a user level?

0

When I create a user the Register AccountController function that is native to Asp.Net MVC frameworks has a feature that I enter that is responsible for giving a user level.

I'm having a problem, which I describe right below.

User is logged in; In the register becomes Advertiser in the table (dbo.AspNetUserRole) Registered user and logged in by default User tries to access AdController which only has the following authorization [Authorize (Roles="Administrator, Advertiser")] And even though having the authorization the user is redirected to login, not being able to access the ad screen.

But if the user scrolls and logs in again he can sign in.

Conclusion, how do I when the Register AccountController function when inserting the user level of a refresh in Asp.Net MVC stating that this user has received a User Level.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

            /*******************************************************
                RESPONSÁVEL EM DAR NÍVEL DE ACESSO
            ********************************************************/
            if (Session["Anunciante"] != null)
            {
                ApplicationDbContext db = new ApplicationDbContext();
                var role = db.Roles.Where(r => r.Name == "Anunciante").SingleOrDefault();

                if (!UserManager.IsInRole(user.Id, role.Name))
                {
                    UserManager.AddToRole(user.Id, role.Name);
                }

                return RedirectToAction("Create", "Empresas");
            }

            return RedirectToAction("Index", "Home");
        }
        AddErrors(result);
    }

    return View(model);
}
    
asked by anonymous 28.10.2018 / 01:14

0 answers