public string Create(aspnetuserroles Model)
        {
            DatabaseContext db = new DatabaseContext();
            string          msg;

            try
            {
                if (ModelState.IsValid)
                {
                    Model.UserId = new Random().Next(1, 1000);
                    db.aspnetuserroles.Add(Model);
                    db.SaveChanges();
                    msg = "Saved Successfully";
                }
                else
                {
                    msg = "Validation data not successfully";
                }
            }
            catch (Exception ex)
            {
                msg = "Error occured:" + ex.Message;
            }
            return(msg);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Register(MultipleClassRegistrar model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.objRegistrar.Email, Email = model.objRegistrar.Email
                };
                var result = await UserManager.CreateAsync(user, model.objRegistrar.Password);

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");

                    ViewBag.Link = callbackUrl;
                    //return View("DisplayEmail");
                }
                AddErrors(result);

                string idcuenta = user.Id;

                aspnetroles asprol = db.aspnetroles.FirstOrDefault(r => r.Name == "Administrador");

                var rol = new aspnetuserroles
                {
                    UserId = idcuenta,
                    RoleId = asprol.Id
                };

                db.aspnetuserroles.Add(rol);
                db.SaveChanges();

                var emp = new empresas
                {
                    Emp_Nom = model.objEmpresas.Emp_Nom
                };
                db.empresas.Add(emp);
                db.SaveChanges();

                int empresa_id = emp.Emp_Id;

                var log = new login
                {
                    Emp_Id = empresa_id,
                    Id     = idcuenta
                };

                db.login.Add(log);
                db.SaveChanges();

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public string Delete(string Id)
        {
            DatabaseContext db             = new DatabaseContext();
            aspnetuserroles aspnetuserrole = db.aspnetuserroles.Find(int.Parse(Id));

            db.aspnetuserroles.Remove(aspnetuserrole);
            db.SaveChanges();
            return("Deleted successfully");
        }
        public string Edit(aspnetuserroles Model)
        {
            DatabaseContext db = new DatabaseContext();
            string          msg;

            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(Model).State = EntityState.Modified;
                    db.SaveChanges();
                    msg = "Saved Successfully";
                }
                else
                {
                    msg = "Validation data not successfully";
                }
            }
            catch (Exception ex)
            {
                msg = "Error occured:" + ex.Message;
            }
            return(msg);
        }
        public async Task <ActionResult> CuentaPersonas(MultiplesClases model, string subemp_id, string per_rut, string car_nom)
        {
            if (ModelState.IsValid)
            {
                string empresa = HttpContext.Session["Empresa"].ToString();

                var user = new ApplicationUser {
                    UserName = model.ObjRegistrar.Email, Email = model.ObjRegistrar.Email
                };
                var result = await UserManager.CreateAsync(user, model.ObjRegistrar.Password);

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                }
                AddErrors(result);

                string   idcuenta = user.Id;
                personas personas = db.personas.FirstOrDefault(p => p.Per_Rut == per_rut);
                //var cargos = db.cargos.FirstOrDefault(c => c.Car_Id == personas.Car_Id);



                aspnetroles asprol = db.aspnetroles.FirstOrDefault(r => r.Name == car_nom);

                aspnetuserroles rol = new aspnetuserroles
                {
                    UserId = idcuenta,
                    RoleId = asprol.Id
                };


                db.aspnetuserroles.Add(rol);
                db.SaveChanges();



                var empresas = db.empresas.FirstOrDefault(e => e.Emp_Nom == empresa);

                int empresa_id = empresas.Emp_Id;

                login log = new login
                {
                    Emp_Id  = empresa_id,
                    Id      = idcuenta,
                    Per_Rut = per_rut
                };

                db.login.Add(log);
                db.SaveChanges();

                //contratos contratos = db.contratos.FirstOrDefault(c=>c.Per_Rut==per_rut);

                //int subemp_id=contratos.Sub_Id;


                return(RedirectToAction("Index", "Contratos", new { subemp_id }));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }