AspNet.Identity Creating user with Role

1

It's the first time I'm working with Entity and Identity and I'm confused about how my user persists. Basically I have a User table, with several relationships with the database and the Id int) I have the Identity AspNetUsers table and AspNetRoles.

I'm trying to create a transaction to ensure that I will not create AspNetUsers without the User and still not add the selected Role in View to a DropDown that I called Profile.

I tried on my UserController the following, in the action create:

[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);
    }

The code that persists in my User table 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 rows for creating the rollback transaction etc. With the transaction as it is I am having a connection timeout error hit. It appears that the transaction is blocking the table, preventing other inserts and validation queries from running. Can anyone identify where I'm going wrong?

    
asked by anonymous 20.11.2017 / 17:52

1 answer

1

Although you instantiated the GPSdEntitiesIdentity class, the UserStore and the UserManager do not use this object or the transaction object that you created .. so this code snippet is half ignored. Home The UserStore class opens and closes a connection at the time of the command, and your business class must do the same thing.
The TransactionScope can help you with what you need.

See if it solves it ... if it does not resolve, let me know.

    
21.11.2017 / 11:03