public static string GenerateOTP(int length)
        {
            kryptoEntities1 krypto = new kryptoEntities1();
            var             res    = krypto.Database.SqlQuery <int>("SELECT * from [krypto].[dbo].fnotpgeneration(1);").ToList();

            return(res[0].ToString());
        }
 public ActionResult SaveRecords()
 {
     try
     {
         UserLoginInformation loggedinUser = LoginController.ActiveUser;
         try
         {
             using (TransactionScope transactionScope = new TransactionScope())
             {
                 try
                 {
                     using (kryptoEntities1 db = new kryptoEntities1())
                     {
                         foreach (var @eachObj in FinalObjects)
                         {
                             @eachObj.FacilityId    = Facility;
                             @eachObj.Organisations = @eachObj.Facility.OrganisationId.ToString();
                             @eachObj.CreatedById   = loggedinUser.USERID.ToString();
                             @eachObj.ModifiedById  = loggedinUser.USERID.ToString();
                             @eachObj.CreatedDate   = DateTime.Now;
                             @eachObj.ModifiedDate  = DateTime.Now;
                             @eachObj.UserIsActive  = @eachObj.IsActive;
                             @eachObj.Status        = 1;
                             @eachObj.IsNormalUser  = true;
                             db.UserLoginInformations.Add(@eachObj);
                             db.SaveChanges();
                             if (@eachObj.IsActive)
                             {
                                 UserActivate useracive = new UserActivate();
                                 useracive.CreatedById = loggedinUser.USERID.ToString();
                                 useracive.Date        = DateTime.Now;
                                 useracive.USERID      = @eachObj.USERID;
                                 useracive.IsActive    = @eachObj.IsActive;
                                 db.UserActivates.Add(useracive);
                                 db.SaveChanges();
                             }
                         }
                     }
                     transactionScope.Complete();
                 }
                 catch (Exception wException)
                 {
                     return(Json("Record saving failed...", JsonRequestBehavior.AllowGet));
                 }
             }
         }
         catch (Exception wException)
         {
             return(Json("Record saving failed...", JsonRequestBehavior.AllowGet));
         }
         return(Json("Records Saved Sucessfully", JsonRequestBehavior.AllowGet));
     }
     catch (Exception)
     {
         return(Json("Record saving failed...", JsonRequestBehavior.AllowGet));
     }
 }
        public static string GenerateOTP(int length)
        {
            var krypto = new kryptoEntities1();
            var res    =
                krypto.Database.SqlQuery <int>(string.Format("SELECT * from [dbo].fnotpgenerationAdmin({0});", length))
                .ToList();

            return(res[0].ToString());
        }
        public void OtpSent(UserLoginInformation ulinfo, int loginUserId)
        {
            var user = _context.UserRegistrationOTPs.SingleOrDefault(u => u.USERID.Equals(ulinfo.USERID));

            if (user == null)
            {
                var number = GenerateOTP(11);
                using (var transactionscope = new TransactionScope())
                {
                    bool value;
                    try
                    {
                        using (var db = new kryptoEntities1()) // Context object
                        {
                            try
                            {
                                var usRegOtp = new UserRegistrationOTP
                                {
                                    USERID       = ulinfo.USERID,
                                    OTP          = number,
                                    Status       = 2,
                                    Notes        = "",
                                    CreatedById  = loginUserId.ToString(),
                                    ModifiedById = loginUserId.ToString(),
                                    CreatedDate  = DateTime.Now,
                                    ModifiedDate = DateTime.Now
                                };
                                db.UserRegistrationOTPs.Add(usRegOtp);
                                db.SaveChanges();
                                value = true;
                                transactionscope.Complete();
                            }
                            catch (Exception)
                            {
                                value = false;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        value = false;
                    }
                    if (value)
                    {
                        SendOTPMail(number, ulinfo.EmailId, ulinfo.FirstName + " " + ulinfo.LastName);
                    }
                }
            }
        }
        public string UpdateChatGroup(ChatGroup groupinfo)
        {
            try
            {
                UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
                if (groupinfo.GroupId == 0)
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    groupinfo.USERID       = loggedinUser.USERID;
                                    groupinfo.ModifiedById = loggedinUser.USERID.ToString();
                                    groupinfo.CreatedById  = loggedinUser.USERID.ToString();
                                    groupinfo.CreatedDate  = DateTime.Now;
                                    groupinfo.Status       = 1;
                                    // groupinfo.GroupType = 1;
                                    db.ChatGroups.Add(groupinfo);
                                    db.SaveChanges();
                                    if (groupinfo.GroupId > 0)
                                    {
                                        List <MyNode> responseNodes =
                                            JsonConvert.DeserializeObject <List <MyNode> >(groupinfo.UserSelections);

                                        foreach (MyNode @node in responseNodes)
                                        {
                                            ChatGroupParticipant participant = new ChatGroupParticipant
                                            {
                                                USERID       = @node.value,
                                                GroupId      = groupinfo.GroupId,
                                                CreatedById  = loggedinUser.USERID.ToString(),
                                                ModifiedById = loggedinUser.USERID.ToString(),
                                                CreatedDate  = DateTime.Now,
                                                ModifiedDate = DateTime.Now,
                                                IsActive     = true,
                                                IsAdmin      = false,
                                                Status       = 1
                                            };
                                            db.ChatGroupParticipants.Add(participant);
                                        }
                                        db.SaveChanges();
                                    }
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
                else
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    groupinfo = Updateobject(groupinfo.GroupId, groupinfo);
                                    groupinfo.ModifiedById    = loggedinUser.USERID.ToString();
                                    groupinfo.ModifiedDate    = DateTime.Now;
                                    db.Entry(groupinfo).State = EntityState.Modified;
                                    db.SaveChanges();

                                    List <MyNode> responseNodes =
                                        JsonConvert.DeserializeObject <List <MyNode> >(groupinfo.UserSelections);
                                    List <ChatGroupParticipant> participantsInDb =
                                        _context.ChatGroupParticipants.Where(x => x.GroupId == groupinfo.GroupId)
                                        .ToList();

                                    List <int> indb         = participantsInDb.Select(x => x.USERID).ToList();
                                    List <int> inselections = responseNodes.Select(x => x.value).ToList();

                                    var toAdd    = UserDatatablesController.ExcludedRight(indb, inselections);
                                    var toDelete = UserDatatablesController.ExcludedLeft(indb, inselections);

                                    foreach (int @id in toAdd)
                                    {
                                        db.ChatGroupParticipants.Add(new ChatGroupParticipant
                                        {
                                            USERID       = @id,
                                            GroupId      = groupinfo.GroupId,
                                            CreatedById  = loggedinUser.USERID.ToString(),
                                            ModifiedById = loggedinUser.USERID.ToString(),
                                            CreatedDate  = DateTime.Now,
                                            ModifiedDate = DateTime.Now,
                                            IsActive     = true,
                                            IsAdmin      = false,
                                            Status       = 1
                                        });
                                    }
                                    foreach (
                                        ChatGroupParticipant existingChatGroupParticipant in
                                        toDelete.Select(
                                            id =>
                                            db.ChatGroupParticipants.SingleOrDefault(
                                                x => x.USERID.Equals(id) && x.GroupId.Equals(groupinfo.GroupId)))
                                        )
                                    {
                                        db.ChatGroupParticipants.Remove(existingChatGroupParticipant);
                                    }
                                    db.SaveChanges();
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }
            return("SUCESS");
        }
Exemplo n.º 6
0
        public string UpdateFacilittMaster(FacilityMaster facilityinfo)
        {
            try
            {
                UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
                if (facilityinfo.FacilityMasterId == 0)
                {
                    facilityinfo.CreatedById  = loggedinUser.USERID.ToString();
                    facilityinfo.ModifiedById = loggedinUser.USERID.ToString();
                    facilityinfo.CreatedDate  = DateTime.Now;
                    facilityinfo.ModifiedDate = DateTime.Now;
                    _context.FacilityMasters.Add(facilityinfo);
                    _context.SaveChanges();
                }
                else
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    facilityinfo = Updateobject(facilityinfo.FacilityMasterId, facilityinfo);
                                    facilityinfo.ModifiedById    = loggedinUser.USERID.ToString();
                                    facilityinfo.ModifiedDate    = DateTime.Now;
                                    db.Entry(facilityinfo).State = EntityState.Modified;
                                    db.SaveChanges();

                                    List <MyNode> responseNodes =
                                        JsonConvert.DeserializeObject <List <MyNode> >(facilityinfo.UserSelections);
                                    List <UserLoginInformation> usersInDb = new List <UserLoginInformation>();

                                    usersInDb.AddRange(facilityinfo.GetAssocaitedFacilityAdmins());

                                    List <int> indb         = usersInDb.Select(x => x.USERID).ToList();
                                    List <int> inselections = responseNodes.Select(x => x.value).ToList();

                                    var toInclude = UserDatatablesController.ExcludedRight(indb, inselections);
                                    var toExclude = UserDatatablesController.ExcludedLeft(indb, inselections);

                                    foreach (int @in in toInclude)
                                    {
                                        UserLoginInformation current = db.UserLoginInformations.Find(@in);
                                        current.IsFacilityAdmin = true;
                                        db.Entry(current).State = EntityState.Modified;
                                    }
                                    foreach (int @out in toExclude)
                                    {
                                        UserLoginInformation current = db.UserLoginInformations.Find(@out);
                                        current.IsFacilityAdmin = false;
                                        db.Entry(current).State = EntityState.Modified;
                                    }
                                    db.SaveChanges();
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }
            return("SUCESS");
        }
        public string ResetPswd(int selecteduser)
        {
            var user        = _context.UserLoginInformations.Single(x => x.USERID.Equals(selecteduser));
            var info        = new KPTY_USER_FORGOT_PASS_OTP_REQ_TBL();
            var intialLogin = new UserRegitrationForInitialLogin();

            Session["OTPCount"] = 0;
            var otp = GenerateOTP(4);

            try
            {
                var loggedinUser = LoginController.ActiveUser;
                info.USERID       = user.USERID;
                info.ModifiedById = loggedinUser.USERID.ToString();
                info.CreatedById  = loggedinUser.USERID.ToString();
                info.CREATED_DATE = DateTime.Now;
                info.ModifiedDate = DateTime.Now;
                info.STATUS       = 1;
                info.OTPVAL       = otp;
                try
                {
                    using (var transactionScope = new TransactionScope())
                    {
                        try
                        {
                            using (var db = new kryptoEntities1()) // Context object
                            {
                                db.Database.ExecuteSqlCommand(
                                    "delete from KPTY_USER_FORGOT_PASS_OTP_REQ_TBL where UserId = {0}", selecteduser);
                                db.KPTY_USER_FORGOT_PASS_OTP_REQ_TBL.Add(info);
                                db.UserRegitrationForInitialLogins.Remove(
                                    db.UserRegitrationForInitialLogins.SingleOrDefault(u => u.USERID == selecteduser));

                                intialLogin.IsInitialLogin    = true;
                                intialLogin.IsTermsAccepted   = false;
                                intialLogin.IsSecQuestEnabled = false;
                                intialLogin.IsPasswordUpdated = false;
                                intialLogin.IsMpinCreated     = false;
                                intialLogin.Notes             = null;
                                intialLogin.Status            = 1;
                                intialLogin.ModifiedById      = loggedinUser.USERID.ToString();
                                intialLogin.ModifiedDate      = DateTime.Now;
                                intialLogin.USERID            = selecteduser;
                                intialLogin.Createdate        = DateTime.Now;
                                intialLogin.CreatedById       = loggedinUser.USERID.ToString();

                                db.UserRegitrationForInitialLogins.Add(intialLogin);
                                db.SaveChanges();
                                db.Database.ExecuteSqlCommand("delete from KPTY_USER_SECQURITY_QUEST_ANS where UserId = {0}", selecteduser);
                                Encryption eny            = new Encryption();
                                string     EncrptPassword = eny.EncryptString(otp.ToString());
                                user.Password        = EncrptPassword;
                                user.ModifiedById    = loggedinUser.USERID.ToString();
                                user.ModifiedDate    = DateTime.Now;
                                db.Entry(user).State = EntityState.Modified;
                                db.SaveChanges();
                            }
                            transactionScope.Complete();
                            // transaction complete
                            var recemail = user.EmailId;
                            if (!SendOTPMail(otp, recemail, user.FirstName))
                            {
                                return("Invalid Email");
                            }
                        }
                        catch (Exception ee)
                        {
                            return("FAIL");
                        }
                    }
                }
                catch (Exception exception)
                {
                    return("FAIL");
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }

            return("SUCESS");
        }
        public string UpdateUser(UserLoginInformation ulinfo)
        {
            try
            {
                var loggedinUser = LoginController.ActiveUser;
                ulinfo.ModifiedById = loggedinUser.USERID.ToString();
                if (ulinfo.USERID == 0)
                {
                    ulinfo.CreatedById = loggedinUser.USERID.ToString();
                    ulinfo.CreatedDate = DateTime.Now;
                    ulinfo.Status      = 1; //insert record status
                    if (ulinfo.IsActive)
                    {
                        ulinfo.UserIsActive = true;
                    }
                    try
                    {
                        using (var transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (var db = new kryptoEntities1()) // Context object
                                {
                                    db.UserLoginInformations.Add(ulinfo);
                                    db.SaveChanges();

                                    if (ulinfo.USERID > 0)
                                    {
                                        if (ulinfo.IsActive)
                                        {
                                            ulinfo.ActivatedDate   = DateTime.Now;
                                            db.Entry(ulinfo).State = EntityState.Modified;
                                            db.SaveChanges();
                                            var useracive = new UserActivate
                                            {
                                                CreatedById = loggedinUser.USERID.ToString(),
                                                Date        = DateTime.Now,
                                                USERID      = ulinfo.USERID,
                                                IsActive    = ulinfo.IsActive,
                                                Status      = 1
                                            };
                                            db.UserActivates.Add(useracive);
                                            db.SaveChanges();
                                        }
                                    }

                                    var otherFacilityIds = ulinfo.OtherFacilityIds;
                                    if (ulinfo.USERID > 0 &&
                                        (otherFacilityIds != null && otherFacilityIds.Length > 0))
                                    {
                                        foreach (var eachid in otherFacilityIds)
                                        {
                                            var facilityid = int.Parse(eachid);
                                            db.UserFacilities.Add(new UserFacility
                                            {
                                                FacilityId   = facilityid,
                                                USERID       = ulinfo.USERID,
                                                Status       = 1,
                                                CreatedById  = loggedinUser.USERID.ToString(),
                                                CreatedDate  = DateTime.Now,
                                                ModifiedDate = DateTime.Now,
                                                ModifiedById = loggedinUser.USERID.ToString()
                                            });
                                        }
                                        db.SaveChanges();
                                    }
                                    if (ulinfo.USERID > 0)
                                    {
                                        var initiallogin =
                                            new UserRegitrationForInitialLogin
                                        {
                                            USERID            = ulinfo.USERID,
                                            Createdate        = DateTime.Now,
                                            IsInitialLogin    = true,
                                            IsTermsAccepted   = false,
                                            IsSecQuestEnabled = false,
                                            IsMpinCreated     = false,
                                            IsPasswordUpdated = false,
                                            Status            = 1,
                                            CreatedById       = loggedinUser.USERID.ToString(),
                                            ModifiedById      = loggedinUser.USERID.ToString()
                                        };
                                        initiallogin.ModifiedDate = initiallogin.Createdate;
                                        db.UserRegitrationForInitialLogins.Add(initiallogin);
                                        db.SaveChanges();
                                    }
                                }
                                transactionScope.Complete(); // transaction complete
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
                else
                {
                    try
                    {
                        using (var transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (var db = new kryptoEntities1())
                                {
                                    ulinfo.ModifiedDate = DateTime.Now;
                                    var prevobj = _context.UserLoginInformations.Find(ulinfo.USERID);
                                    if (prevobj.IsActive != ulinfo.IsActive)
                                    {
                                        var activate = new UserActivate
                                        {
                                            IsActive    = !prevobj.IsActive,
                                            CreatedById = loggedinUser.USERID.ToString()
                                        };
                                        if (ulinfo.IsActive)
                                        {
                                            ulinfo.ActivatedDate = DateTime.Now;
                                            activate.Date        = ulinfo.ActivatedDate;
                                        }
                                        else
                                        {
                                            ulinfo.DeactivatedDate = DateTime.Now;
                                            activate.Date          = ulinfo.DeactivatedDate;
                                        }
                                        activate.USERID = prevobj.USERID;
                                        db.UserActivates.Add(activate);
                                    }
                                    ulinfo.UserIsActive = ulinfo.IsActive;
                                    ulinfo = Updateobject(ulinfo.USERID, ulinfo);
                                    db.Entry(ulinfo).State = EntityState.Modified;
                                    db.SaveChanges();

                                    var otherFacilityIdsAsints        = ulinfo.GetOtherFacilityIdsAsints();
                                    var facilityIdsInUserFacilityList = ulinfo.GetFacilityIdsInUserFacilityList();
                                    var toAdd    = ExcludedRight(facilityIdsInUserFacilityList, otherFacilityIdsAsints);
                                    var toDelete = ExcludedLeft(facilityIdsInUserFacilityList, otherFacilityIdsAsints);
                                    foreach (var @id in toAdd)
                                    {
                                        db.UserFacilities.Add(new UserFacility
                                        {
                                            FacilityId   = @id,
                                            USERID       = ulinfo.USERID,
                                            Status       = 1,
                                            CreatedById  = loggedinUser.USERID.ToString(),
                                            CreatedDate  = DateTime.Now,
                                            ModifiedDate = DateTime.Now,
                                            ModifiedById = loggedinUser.USERID.ToString()
                                        });
                                    }
                                    foreach (
                                        var existingUserFacility in
                                        toDelete.Select(
                                            id =>
                                            db.UserFacilities.SingleOrDefault(
                                                x =>
                                                x.FacilityId.Value.Equals(id) &&
                                                x.USERID.Equals(ulinfo.USERID))))
                                    {
                                        db.UserFacilities.Remove(existingUserFacility);
                                    }
                                    db.SaveChanges();
                                    if (ulinfo.IsActive == false)
                                    {
                                        RemoveUser(ulinfo.USERID);
                                    }
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
                if (ulinfo.USERID > 0 && ulinfo.IsActive)
                {
                    OtpSent(ulinfo, loggedinUser.USERID);
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }
            return("SUCESS");
        }
        public ActionResult UpdateUserStatus(int currentRecord, bool currentStatus)
        {
            var info         = _context.UserLoginInformations.Single(x => x.USERID == currentRecord && x.Status == 1);
            var loggedinUser = LoginController.ActiveUser;

            if (info.IsActive != currentStatus)
            {
                info.IsActive     = currentStatus;
                info.UserIsActive = currentStatus;
                try
                {
                    using (var transactionScope = new TransactionScope())
                    {
                        try
                        {
                            using (kryptoEntities1 db = new kryptoEntities1()) // Context object
                            {
                                if (info.IsActive)
                                {
                                    info.ActivatedDate = DateTime.Now;
                                }
                                else
                                {
                                    info.DeactivatedDate = DateTime.Now;
                                }
                                db.Entry(info).State = EntityState.Modified;
                                db.SaveChanges();
                                var useracive = new UserActivate
                                {
                                    CreatedById = loggedinUser.USERID.ToString(),
                                    Date        = DateTime.Now,
                                    USERID      = info.USERID,
                                    IsActive    = info.IsActive
                                };
                                db.UserActivates.Add(useracive);
                                db.SaveChanges();
                            }
                            transactionScope.Complete();
                        }
                        catch (Exception Wx)
                        {
                            return(Json("Something went Wrong!", JsonRequestBehavior.AllowGet));
                        }
                    }
                    if (info.USERID > 0 && info.IsActive)
                    {
                        OtpSent(info, loggedinUser.USERID);
                    }
                    else if (info.IsActive == false)
                    {
                        RemoveUser(info.USERID);
                    }
                }
                catch (Exception Ex)
                {
                    return(Json("Something went Wrong!", JsonRequestBehavior.AllowGet));
                }
                return(Json("Sucessfully Updated the Status", JsonRequestBehavior.AllowGet));
            }
            return(Json("No Changes to Update", JsonRequestBehavior.AllowGet));
        }
        public string ResetOtp(int selectedUser)
        {
            var loggedinUser = LoginController.ActiveUser;
            var user         = _context.UserLoginInformations.Single(x => x.USERID.Equals(selectedUser));
            var userOtp      = new UserForgotMpinsOTP();
            var number       = GenerateOTP(11);

            try
            {
                using (var transactionScope = new TransactionScope())
                {
                    try
                    {
                        using (var db = new kryptoEntities1()) // Context object
                        {
                            userOtp.USERID       = selectedUser;
                            userOtp.OTPVAL       = number;
                            userOtp.STATUS       = 2;
                            userOtp.Notes        = "";
                            userOtp.CREATED_DATE = DateTime.Now;
                            userOtp.ModifiedDate = DateTime.Now;
                            userOtp.CreatedById  = loggedinUser.USERID.ToString();
                            userOtp.ModifiedById = loggedinUser.USERID.ToString();

                            db.Database.ExecuteSqlCommand("delete from UserForgotMpinsOTPS where UserId = {0}",
                                                          selectedUser);

                            db.UserForgotMpinsOTPS.Add(userOtp);
                            db.SaveChanges();
                        }

                        transactionScope.Complete();



                        bool x1 = SendOTPMail(number, user.EmailId, user.FirstName);
                        if (!x1)
                        {
                            return("Invalid Email");
                        }
                    }
                    catch (Exception ee)
                    {
                        return("FAIL");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }

            return("SUCESS");
        }
        public string ResetOtp(int selectedUser)
        {
            UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
            UserLoginInformation user         = _context.UserLoginInformations.SingleOrDefault(x => x.USERID.Equals(selectedUser));
            UserForgotMpinsOTP   UserOtp      = new UserForgotMpinsOTP();
            string Number = GenerateOTP(11);

            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        using (kryptoEntities1 db = new kryptoEntities1())     // Context object
                        {
                            UserOtp.USERID       = selectedUser;
                            UserOtp.OTPVAL       = Number;
                            UserOtp.STATUS       = 1;
                            UserOtp.Notes        = "";
                            UserOtp.CREATED_DATE = DateTime.Now;
                            UserOtp.ModifiedDate = DateTime.Now;
                            UserOtp.CreatedById  = loggedinUser.USERID.ToString();
                            UserOtp.ModifiedById = loggedinUser.USERID.ToString();

                            db.Database.ExecuteSqlCommand("delete from UserForgotMpinsOTPS where UserId = {0}", selectedUser);

                            db.UserForgotMpinsOTPS.Add(UserOtp);
                            db.SaveChanges();
                        }

                        transactionScope.Complete();

                        var msg = "Dear User, \n\n Your request to process the reset OTP is successful and your new OTP generated is  " + Number + " .  \n\n This is system generated message please do not reply.";

                        bool x1 = SendEmail("Your OTP has been reset", msg, user.EmailId, user.FirstName);
                        if (!x1)
                        {
                            return("Invalid Email");
                        }
                    }

                    catch (Exception ee)
                    {
                        return("FAIL");
                    }
                }
            }

            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }

            return("SUCESS");
        }
        public string ResetPswd(int selecteduser)
        {
            UserLoginInformation user = _context.UserLoginInformations.SingleOrDefault(x => x.USERID.Equals(selecteduser));
            KPTY_USER_FORGOT_PASS_OTP_REQ_TBL info        = new KPTY_USER_FORGOT_PASS_OTP_REQ_TBL();
            UserRegitrationForInitialLogin    IntialLogin = new UserRegitrationForInitialLogin();

            Session["OTPCount"] = 0;
            var OTP = GenerateOTP(4);

            try
            {
                UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
                //ResetOTPStatus(loggedinUser.USERID);
                info.USERID       = user.USERID;
                info.ModifiedById = loggedinUser.USERID.ToString();
                info.CreatedById  = loggedinUser.USERID.ToString();
                info.CREATED_DATE = DateTime.Now;
                info.ModifiedDate = DateTime.Now;
                info.STATUS       = 1;
                info.OTPVAL       = OTP;
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        try
                        {
                            using (kryptoEntities1 db = new kryptoEntities1()) // Context object
                            {
                                db.Database.ExecuteSqlCommand("delete from KPTY_USER_FORGOT_PASS_OTP_REQ_TBL where UserId = {0}", selecteduser);

                                db.KPTY_USER_FORGOT_PASS_OTP_REQ_TBL.Add(info);
                                db.SaveChanges();

                                db.Database.ExecuteSqlCommand("delete from UserRegitrationForInitialLogin where UserId = {0}", selecteduser);

                                IntialLogin.IsInitialLogin    = true;
                                IntialLogin.IsTermsAccepted   = false;
                                IntialLogin.IsSecQuestEnabled = false;
                                IntialLogin.IsPasswordUpdated = false;
                                IntialLogin.IsMpinCreated     = false;
                                IntialLogin.Notes             = null;
                                IntialLogin.Status            = 1;
                                IntialLogin.ModifiedById      = loggedinUser.USERID.ToString();
                                IntialLogin.ModifiedDate      = DateTime.Now;
                                IntialLogin.USERID            = selecteduser;
                                IntialLogin.Createdate        = DateTime.Now;
                                IntialLogin.CreatedById       = loggedinUser.USERID.ToString();

                                db.UserRegitrationForInitialLogins.Add(IntialLogin);
                                db.SaveChanges();
                            }
                            transactionScope.Complete();
                            // transaction complete
                            var recemail = user.EmailId;
                            var msg      = "Dear User,\n\n Your request to process the reset password is successful and your new OTP generated is  " + OTP + " . Please use it to login again. \n This is system generated message please do not reply.";

                            bool x1 = SendEmail("Your Password has been reset", msg, recemail, user.FirstName);
                            if (!x1)
                            {
                                return("Invalid Email");
                            }
                        }

                        catch (Exception ee)
                        {
                            return("FAIL");
                        }
                    }
                }
                catch (Exception exception)
                {
                    return("FAIL");
                }
            }

            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }


            return("SUCESS");
        }