public ActionResult ParticipantProfile(participant_profile participant, HttpPostedFileBase file)
        {
            var participantRepo = new ParticipiantRepository();
            var oparticipant    = participantRepo.Get(participant.Id);

            oparticipant.Mobile            = participant.Mobile;
            oparticipant.Gender            = participant.Gender;
            oparticipant.DateOfBirth       = participant.DateOfBirth;
            oparticipant.ProgrammName1     = participant.ProgrammName1;
            oparticipant.FatherMobile      = participant.Mobile;
            oparticipant.City              = participant.City;
            oparticipant.SchoolId          = participant.SchoolId;
            oparticipant.FacebookAddress   = participant.FacebookAddress;
            oparticipant.TwitterAddress    = participant.TwitterAddress;
            oparticipant.SnapChatAddress   = participant.SnapChatAddress;
            oparticipant.Stage             = participant.Stage;
            oparticipant.user.FirstLogin   = true;
            oparticipant.IsProfileComplete = true;
            oparticipant.Instagram         = participant.Instagram;
            oparticipant.Class             = participant.Class;
            if (file != null)
            {
                string fileName = "~/Uploads/ImageLibrary/" + Guid.NewGuid() + Path.GetExtension(file.FileName);
                string filePath = Server.MapPath(fileName);
                file.SaveAs(filePath);
                oparticipant.PhotoPath = fileName;
            }
            participantRepo.Put(participant.Id, oparticipant);
            return(RedirectToAction("Index", "Session"));
        }
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (Request["button"] != null)
            {
                return(RedirectToAction("VolunteerProfile", "volunteer"));
            }

            var a          = 0;
            var repository = new AccountRepository();
            var user       = repository.Get().FirstOrDefault(x => x.Username == model.Username.Trim() && !x.IsLocked);

            if (user == null)
            {
                var participantRepo = new ParticipiantRepository();
                var participant     = participantRepo.Get().FirstOrDefault(x => x.NationalID == model.Username && x.isActive);
                if (participant != null)
                {
                    user = participant.user;
                }
            }
            if (user != null)
            {
                var password1 = EncryptionKeys.Decrypt(user.Password);
                var password  = EncryptionKeys.Encrypt(model.Password);
                if (user.Password.Equals(password))
                {
                    var    role     = new RoleRepository().Get(user.RoleId);
                    var    enumRole = (EnumUserRole)role.Code;
                    string route    = Request.Form["route"];
                    if (route == "manager" && enumRole != EnumUserRole.SuperAdmin)
                    {
                        return(RedirectToAction("Admin", new { error = true }));
                    }
                    if (route != "manager" && enumRole == EnumUserRole.SuperAdmin)
                    {
                        return(RedirectToAction("Login", new { error = true }));
                    }
                    if (enumRole == EnumUserRole.Coordinator)
                    {
                    }
                    var cu = new ContextUser
                    {
                        OUser     = user,
                        EnumRole  = enumRole,
                        Role      = role,
                        PhotoPath = "/img/avatars/admin.png"
                    };

                    Session["user"] = cu;
                    FormsAuthentication.SetAuthCookie(user.Username, false);
                    //var claims = new List<Claim>();
                    //claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Username));
                    //claims.Add(new Claim(ClaimTypes.Name, user.FirstName));
                    //claims.Add(new Claim(ClaimTypes.Email, user.Email));
                    //claims.Add(new Claim(ClaimTypes.Role, userRole.ToString("g")));
                    //claims.Add(new Claim(ClaimTypes.Sid, user.Id.ToString()));

                    //var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

                    //var ctx = Request.GetOwinContext();
                    //var authenticationManager = ctx.Authentication;
                    //authenticationManager.SignIn(id);


                    return(RedirectToPortal(enumRole, user));
                }
            }

            string route1 = Request.Form["route"];

            if (route1 == "manager")
            {
                return(RedirectToAction("Admin", new { error = true }));
            }
            if (route1 != "manager")
            {
                return(RedirectToAction("Login", new { error = true }));
            }

            return(View(model));

            //// This doesn't count login failures towards account lockout
            //// To enable password failures to trigger account lockout, change to shouldLockout: true
            //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            //switch (result)
            //{
            //    case SignInStatus.Success:
            //        return RedirectToLocal(returnUrl);
            //    case SignInStatus.LockedOut:
            //        return View("Lockout");
            //    case SignInStatus.RequiresVerification:
            //        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            //    case SignInStatus.Failure:
            //    default:
            //        ModelState.AddModelError("", "Invalid login attempt.");
            //        return View(model);
            //}
        }
        public ActionResult Edit(participant_profile profile)
        {
            var accountRepo                 = new AccountRepository();
            var participantRepo             = new ParticipiantRepository();
            participant_profile participant = null;
            var cu = Session["user"] as ContextUser;

            if (profile.Id == 0)
            {
                if (accountRepo.EmailExist(profile.Email))
                {
                    ViewBag.EmailExist = true;
                    return(View(profile));
                }
                participant = participantRepo.GetParticipant(profile.NationalID);
                if (participant == null)
                {
                    participant = new participant_profile
                    {
                        RowGuid   = Guid.NewGuid(),
                        CreatedAt = DateTime.Now,
                        CreatedBy = cu.OUser.Id,
                        Email     = profile.Email,
                    };
                }
                if (profile.SessionId > 0)
                {
                    participant.session_participant.Add(new session_participant {
                        SessionID = profile.SessionId, ParticipantID = participant.Id
                    });
                }
            }
            else
            {
                participant           = participantRepo.Get(profile.Id);
                participant.UpdatedAt = DateTime.Now;
                participant.UpdatedBy = cu.OUser.Id;
            }

            var userRole = new RoleRepository().Get().Where(x => x.Code == (int)EnumUserRole.Participant).FirstOrDefault();

            if (participant.ParticipantUserID == 0)
            {
                participant.user = new user
                {
                    RowGuid          = Guid.NewGuid(),
                    Email            = profile.Email,
                    Username         = profile.Email,
                    RegistrationDate = DateTime.Now,
                    FirstName        = profile.Name,
                    RoleId           = userRole.Id,
                    CreatedAt        = DateTime.Now,
                    ValidFrom        = DateTime.Now,
                    FirstLogin       = false,
                    IsMobileVerified = false,
                    IsEmailVerified  = false,
                    CreatedBy        = cu.OUser.Id,
                    Password         = EncryptionKeys.Encrypt(profile.Password)
                }
            }
            ;
            participant.Name       = profile.Name;
            participant.FatherName = profile.FatherName;
            participant.Family     = profile.Family;
            participant.NationalID = profile.NationalID;
            if (profile.MobileNo != null)
            {
                participant.Mobile = profile.MobileNo;
            }
            else
            {
                participant.Mobile = profile.Mobile;
            }
            participant.isActive      = profile.isActive;
            participant.user.IsLocked = !participant.isActive;
            if (participant.Id == 0)
            {
                string             url             = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Login";
                var                bogusController = Util.CreateController <EmailTemplateController>();
                EmailTemplateModel model           = new EmailTemplateModel {
                    Title = "Complete Profile", RedirectUrl = url, UserName = participant.Email, Password = EncryptionKeys.Decrypt(participant.user.Password), ParticipantName = participant.Name, User = participant.user.FirstName
                };
                string body = Util.RenderViewToString(bogusController.ControllerContext, "ParticipantProfile", model);
                EmailSender.SendSupportEmail(body, participant.Email);
                participant.IsEmailSent = true;
                participantRepo.Post(participant);
            }
            else
            {
                participantRepo.Put(participant.Id, participant);
            }
            if (Request["participant"] == "true")
            {
                var rowId = new SessionRepository().Get(profile.SessionId).RowGUID;
                return(RedirectToAction("Edit", "Session", new { id = rowId }));
            }
            return(RedirectToAction("Index"));
        }