public async Task <ActionResult> Registrar(RegisterViewModel model, FormCollection form)
        {
            investicEntities db = new investicEntities();

            model.UserName = model.PersonalID;
            ChamiloUser chuser = new ChamiloUser();

            chuser.FirstName         = model.Name;
            chuser.LasttName         = model.SureName;
            chuser.Password          = model.Password;
            chuser.Email             = model.Mail;
            chuser.Phone             = model.Phone;
            chuser.Username          = model.PersonalID.Trim();
            chuser.Status            = getStatus(form["SelectedUserRoleId"].ToString());
            chuser.Registration_date = DateTime.Now;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName    = model.UserName,
                    Name        = model.Name,
                    SureName    = model.SureName,
                    PersonalID  = model.PersonalID,
                    Genre       = model.Genre,
                    Email       = model.Mail,
                    PhoneNumber = model.Phone,
                    Address     = model.Address,
                    BirthDay    = model.BirthDay
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    IchamiloClient client = new IchamiloClient();
                    client.Open();
                    AspNetUsers aspNetUser = db.AspNetUsers.Where(m => m.PersonalID == user.PersonalID).FirstOrDefault();
                    if (aspNetUser != null)
                    {
                        aspNetUser.TipoDoc         = model.TipoDoc;
                        db.Entry(aspNetUser).State = System.Data.Entity.EntityState.Modified;
                    }
                    AspNetUserRoles asignRoleToUser = new AspNetUserRoles();
                    string          roleSelected    = form["SelectedUserRoleId"].ToString();
                    asignRoleToUser.RoleId = AspNetRoles.GetRoleId(roleSelected);
                    asignRoleToUser.UserId = AspNetUsers.GetUserId(model.UserName);
                    db.AspNetUserRoles.Add(asignRoleToUser);
                    db.SaveChanges();
                    client.insertUser(chuser);
                    try
                    {
                        var m = new MailMessage(
                            new MailAddress("*****@*****.**", "Registro Plataforma Investic"),
                            new MailAddress(user.Email));
                        m.Subject = "Plataforma Investic - Confirmación de correo electronico";
                        m.Body    =
                            string.Format(
                                "Estimado {0}<BR/>Gracias por su registro en la plataforma Investic,<BR/>Sus datos de acceso:<BR/>Usuario: " +
                                model.PersonalID + "<BR/>Contraseña: " + model.Password +
                                "<BR/>Por favor haga clic en el siguiente vinculo, para confirmar su registro: <a href=\"{1}\" title=\"Confirmación Usuario\">{1}</a>",
                                user.UserName,
                                Url.Action("ConfirmarCorreo", "Usuario", new { Token = user.Id, user.Email }, Request.Url.Scheme));
                        m.IsBodyHtml = true;
                        var smtp = new SmtpClient("smtp.gmail.com");
                        smtp.Credentials = new NetworkCredential("*****@*****.**", "Investic666");
                        smtp.EnableSsl   = true;
                        smtp.Send(m);
                        //await SignInAsync(user, isPersistent: false);

                        return(RedirectToAction("EmailEnviado", "Usuario"));
                    }
                    catch {
                        if (AspNetUsersRoles.IsUserInRole("Editor", user.UserName))
                        {
                            return(RedirectToAction("Editor", "Admin", new { nombreu = user.UserName }));
                        }

                        else if (AspNetUsersRoles.IsUserInRole("Maestro", user.UserName))
                        {
                            return(RedirectToAction("Maestro", "Admin", new { nombreu = user.UserName }));
                        }
                        else if (AspNetUsersRoles.IsUserInRole("Estudiante", user.UserName))
                        {
                            return(RedirectToAction("Estudiante", "Admin", new { nombreu = user.UserName }));
                        }
                        else if (AspNetUsersRoles.IsUserInRole("Administrador", user.UserName))
                        {
                            return(RedirectToAction("Index", "Seguimiento"));
                        }
                        else if (AspNetUsersRoles.IsUserInRole("Coordinador", user.UserName))
                        {
                            return(RedirectToAction("Index", "Seguimiento"));
                        }
                        else if (AspNetUsersRoles.IsUserInRole("Contratista", user.UserName))
                        {
                            return(RedirectToAction("Index", "Seguimiento"));
                        }
                    }
                }
                else
                {
                    AddErrors(result);
                    return(RedirectToAction("Registrar", new { code = 20 }));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> RegistroAdmin(RegisterViewModel model, FormCollection form)
        {
            investicEntities db = new investicEntities();

            model.UserName = model.PersonalID;


            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName    = model.UserName,
                    Name        = model.Name,
                    SureName    = model.SureName,
                    PersonalID  = model.PersonalID,
                    Genre       = model.Genre,
                    Email       = model.Mail,
                    PhoneNumber = model.Phone,
                    Address     = model.Address,
                    BirthDay    = model.BirthDay
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    AspNetUserRoles asignRoleToUser = new AspNetUserRoles();
                    string          roleSelected    = form["SelectedUserRoleId"].ToString();
                    asignRoleToUser.RoleId = AspNetRoles.GetRoleId(roleSelected);
                    asignRoleToUser.UserId = AspNetUsers.GetUserId(model.UserName);
                    db.AspNetUserRoles.Add(asignRoleToUser);
                    db.SaveChanges();


                    var m = new MailMessage(
                        new MailAddress("*****@*****.**", "Registro Plataforma Investic"),
                        new MailAddress(user.Email));
                    m.Subject = "Plataforma Investic - Confirmación de correo electronico";
                    m.Body    =
                        string.Format(
                            "Estimado {0}<BR/>Gracias por su registro en la plataforma Investic,<BR/>Sus datos de acceso:<BR/>Usuario: " +
                            model.PersonalID + "<BR/>Contraseña: " + model.Password +
                            "<BR/>Por favor haga clic en el siguiente vinculo, para confirmar su registro: <a href=\"{1}\" title=\"Confirmación Usuario\">{1}</a>",
                            user.UserName,
                            Url.Action("ConfirmarCorreo", "Usuario", new { Token = user.Id, user.Email }, Request.Url.Scheme));
                    m.IsBodyHtml = true;
                    var smtp = new SmtpClient("smtp.gmail.com");
                    smtp.Credentials = new NetworkCredential("*****@*****.**", "Investic666");
                    smtp.EnableSsl   = true;
                    smtp.Send(m);
                    //await SignInAsync(user, isPersistent: false);

                    return(RedirectToAction("EmailEnviado", "Usuario"));
                }
                else
                {
                    AddErrors(result);
                }
            }

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