Aspnet.Identity Creating user with Role

Asked

Viewed 341 times

1

It’s the first time I’m working with Entity and Identity and I’m confused about how to persist my user. Basically I have a table User, with several relationships with the bank and Id int) I have the table Aspnetusers Identity and Aspnetroles.

I am trying to create a transaction to ensure that I will not create Aspnetusers without the User and without adding the Role selected in the View in a Dropdown I called Profile.

I tried in my user controller the following, in the create action:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CriarUsuario(Usuario model)
    {
        if (ModelState.IsValid)
        {
            ApplicationUser identityUser = new ApplicationUser
            {
                UserName = model.LOGIN,
                LOGIN = model.LOGIN,
            };

            using (GPSdEntitiesIdentity IdentityContext = new GPSdEntitiesIdentity())
            {
                using (var transaction = IdentityContext.Database.BeginTransaction())
                {
                    var userStore = new UserStore<ApplicationUser>(IdentityContext);
                    var userManager = new UserManager<ApplicationUser>(userStore);

                    IdentityResult resultado = userManager.Create(identityUser, model.SENHA);
                    if (resultado.Succeeded)
                    {
                        resultado = userManager.AddToRole(identityUser.Id, model.Perfil);
                        if (resultado.Succeeded)
                        {
                            if (GravarNaTabelausuario(identityUser.Id, model))
                            {
                                transaction.Commit();
                                return RedirectToAction("Index", "Home");
                            }
                        }
                    }
                    transaction.Rollback();
                    ModelState.AddModelError("ErroIdentity", resultado.Errors.FirstOrDefault());
                }
            }                
        }
        ViewBag.ReadOnly = false;
        return View(model);
    }

Since the code that persists in my table User is:

public bool GravarNaTabelausuario(string IdUsuario, Usuario model)
    {
        UsuarioEnt UsuarioEnt = new UsuarioEnt()
        {
            IdAspNetUsers = IdUsuario,
            IdPessoa = model.UsuarioEnt.IdPessoa,
            IdStatusRegistro = model.UsuarioEnt.IdStatusRegistro,
            IdUnidade = model.UsuarioEnt.IdUnidade,
        };
        UsuarioNeg UsuarioApp = new UsuarioApp(null);
        if (UsuarioNeg.RecebeRegistro(UsuarioEnt))
        {
            if(UsuarioNeg.ValidaRegistro())
            {
                if (UsuarioNeg.GravaRegistro(UsuarioEnt))
                {
                    return true;
                }
            }
        }            
        return false;
    }

But it only works if I comment on the lines referring to creating the rollback transaction etc. With the transaction as I am having a connection timeout error reached. It seems that the transaction is blocking the table preventing the other inserts and validation queries to be executed. Can anyone identify where I’m going wrong?

1 answer

1


Although you instantiated the Gpsdentitiesidentity class, Userstore and Usermanager do not use this object or the transaction object you created.. so this chunk of code is being kind of ignored.

The Userstore class opens and closes a connection at the time of the command, and your business class should do the same thing.
The Transactionscope can help in what you need.

See if you can fix it... if you can’t fix it, let me know.

  • Thanks Will! I’ll take a look!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.