public async Task <String> AddAdmnistrator(RegisterViewModel model)
        {
            model.Password = System.Web.Security.Membership.GeneratePassword(8, 1);
            var user = new ApplicationUser {
                UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName
            };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var Role = db.Roles.Where(r => r.Id == model.RoleID).SingleOrDefault();
                await UserManager.AddToRoleAsync(user.Id, Role.Name);

                var oUser = db.Users.Find(user.Id);
                oUser.EmailConfirmed = true;
                oUser.LockoutEnabled = false;

                db.Entry(oUser).State = EntityState.Modified;

                EmailController ec = new EmailController();
                ec.SendInvitation(model.Email, model.Email, model.Password);

                await db.SaveChangesAsync();

                return(oUser.Id);
            }

            return(string.Empty);
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create(Agent_Empresa agent_Empresa)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //variables iniciales
                    var CompanyId = Guid.NewGuid();

                    //Creacion de la empresa
                    agent_Empresa.IdCompany = CompanyId;
                    db.Agent_Empresa.Add(agent_Empresa);

                    //Crear Usuario Admnistrador a la empresa
                    var _username = String.Concat("Ad_", agent_Empresa.Nombre.Replace(" ", "").Replace(".", "").ToLower().ToString());
                    var user      = new ApplicationUser {
                        UserName = _username, Email = agent_Empresa.Email, FirstName = agent_Empresa.Nombre, LastName = agent_Empresa.Rut
                    };
                    var Password = System.Web.Security.Membership.GeneratePassword(8, 1);
                    var result   = await UserManager.CreateAsync(user, Password);

                    if (result.Succeeded)
                    {
                        var Role = db.Roles.Where(r => r.Name.Equals("Admin")).SingleOrDefault();
                        await UserManager.AddToRoleAsync(user.Id, Role.Name);

                        var oUser = db.Users.Find(user.Id);
                        oUser.EmailConfirmed = true;
                        oUser.LockoutEnabled = false;

                        db.Entry(oUser).State = EntityState.Modified;

                        var ec = new EmailController();
                        ec.SendInvitation(oUser.Email, oUser.Email, Password);

                        await db.SaveChangesAsync();

                        var relation = new Agent_UserCompanies()
                        {
                            idUser    = Guid.Parse(oUser.Id),
                            IdCompany = CompanyId,
                        };

                        db.Agent_UserCompany.Add(relation);
                        db.SaveChanges();
                    }
                }
                //Rollback
                catch (Exception ex)
                {
                    if (agent_Empresa != null)
                    {
                        db.Agent_Empresa.Remove(agent_Empresa);
                        db.SaveChanges();
                    }
                }
            }
            return(RedirectToAction("Index", "Agent_Empresa"));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            model.Password = System.Web.Security.Membership.GeneratePassword(8, 1);
            var user = new ApplicationUser {
                UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName
            };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var Role = db.Roles.Where(r => r.Id == model.RoleID).SingleOrDefault();
                await UserManager.AddToRoleAsync(user.Id, Role.Name);

                var oUser = db.Users.Find(user.Id);
                oUser.EmailConfirmed = true;
                oUser.LockoutEnabled = false;

                db.Entry(oUser).State = EntityState.Modified;
                //model.Email = "*****@*****.**";
                //model.Email = "*****@*****.**";

                EmailController ec = new EmailController();
                ec.SendInvitation(model.Email, model.Email, model.Password);

                await db.SaveChangesAsync();

                //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                //string 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 <a href=\"" + callbackUrl + "\">here</a>");

                return(RedirectToAction("UserList", "Manage"));
            }

            List <SelectListItem> RoleID = new List <SelectListItem>();
            var RolesNames = db.Roles.OrderBy(c => c.Name).ToList();

            foreach (var r in RolesNames)
            {
                RoleID.Add(new SelectListItem()
                {
                    Text = r.Name, Value = r.Id
                });
            }

            this.ViewBag.RoleID = new SelectList(RoleID, "Value", "Text");
            // If we got this far, something failed, redisplay form
            return(View(model));
        }