예제 #1
0
 public ActionResult Close(string id)
 {
     using (SystemDbContext mydb = new SystemDbContext()) {
         List<UserModels> tmp = mydb.Users.Where(p => p.CompanyId.Equals(id)).ToList();
         foreach(UserModels user in tmp)
             user.IsProved = false;
         mydb.Entry(tmp).State = System.Data.Entity.EntityState.Modified;
         mydb.SaveChanges();
         return RedirectToAction("Index");
     }
 }
예제 #2
0
        public JsonResult GenerateVerifyCode()
        {
          
            var PhoneNumber = Request["PhoneNumber"];
            string code = (new Random().Next(1111, 9999)).ToString();

            string content = "盆景办公提醒您,您的验证码为:" + code;  
            //向用户发送验证码
       //    int rect  = sendOnce(PhoneNumber, content);
            int rect = 1;
            //短信验证发送失败!
           if (rect <= 0)
           {
               return Json(new
               {
                   errorcode = rect
               });
           }

           using (var vCode = new SystemDbContext())
           {
               var tmp = vCode.VerifyCodes.Find(PhoneNumber);
               DateTime createTime = System.DateTime.Now;
               //电话在数据库中已经存在,更新Code和CreateTime以及OverTime就可以
               if (tmp != null)
               {
                   tmp.Code = code;
                   tmp.CreateTime = createTime;
                   tmp.OverTime = createTime.AddSeconds(VerifyCodeOverTimeSeconds);
                   vCode.Entry(tmp).State = System.Data.Entity.EntityState.Modified;
                   vCode.SaveChanges();
               }
               else
               {
                   VerifyCode tmpCode = new VerifyCode();
                   tmpCode.Code = code;
                   tmpCode.PhoneNumber = PhoneNumber;
                   tmpCode.CreateTime = createTime;
                   tmpCode.OverTime = createTime.AddSeconds(VerifyCodeOverTimeSeconds);
                   vCode.VerifyCodes.Add(tmpCode);
                   vCode.SaveChanges();
               }
           }
            return Json(new {
                 errorcode="1"});
        }
예제 #3
0
        public ActionResult ForgotPasswordApply(UserPasswordInfo info, HttpPostedFileBase image)
        {
            SystemDbContext db = new SystemDbContext();
            if (ModelState.IsValid)
            {
                //UserPasswordInfo info = new UserPasswordInfo();
                db.UserPasswordInfos.Add(info);
                if (image != null)
                {
                    info.BusinessLicenseType = image.ContentType;//获取图片类型
                    //view.BusinessLicence = new byte[image.ContentLength];//新建一个长度等于图片大小的二进制地址
                    // image.InputStream.Read(view.BusinessLicence, 0, image.ContentLength);//将image读取到Logo中
                    info.BusinessLicense = new byte[image.ContentLength];
                    image.InputStream.Read(info.BusinessLicense, 0, image.ContentLength);
                }

                DateTime time = DateTime.Now;

                /*利用Expression表达式树可以解决:LINQ to Entities 不识别方法“System.DateTime AddMinutes(Double)*/
                Expression<Func<UserPasswordInfo, bool>> where = p => p.UserName == info.UserName && (p.SubmitTime.AddMinutes(2) >= time);
                // var item = from p in db.UserPasswordInfos where (p.UserName == info.UserName && (p.SubmitTime.AddMinutes(2)<=time)) select p;
                var item = db.UserPasswordInfos.Where(where.Compile()).ToList();
                if (item.Count() == 0)
                {
                    /*审核状态字段*/
                   // info.AuditStatus = "ture";
                    info.SubmitTime = time;
                    db.SaveChanges();
                   // return RedirectToAction("ForgotPasswordInfo");
                    return RedirectToAction("ForgotPassword");
                }
                else
                    return RedirectToAction("ForgotPasswordInfoError");
            }
            return View(info);
        }
예제 #4
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //首先核对用户的短信验证码是否合法
                using (var vCode = new SystemDbContext())
                {
                    var CurrentUserCode = vCode.VerifyCodes.Find(model.PhoneNumber);
                    DateTime CurTime = System.DateTime.Now;
                    if (CurTime > CurrentUserCode.OverTime)     //用户短信验证码超时
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码已经过期!");
                        return View(model);
                    }
                    else if (!CurrentUserCode.Code.Equals(model.Code))
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码输入错误!");
                        return View(model);
                    }
                }

                //验证企业全称是否已经被注册
                using (var vUser = new SystemDbContext())
                {
                    var tmp = vUser.Users.Where(p => p.CompanyFullName.Equals(model.CompanyFullName)).ToList();
                    if (tmp.Count != 0)
                    {
                        ModelState.AddModelError("", "抱歉,该企业全称已经被注册!");
                        return View(model);

                    }
                }
                var user = new ApplicationUser
                {
                    CompanyFullName = model.CompanyFullName,
                    PhoneNumber = model.PhoneNumber,

                    UserName = model.PhoneNumber,
                    IsProved = false,           //是否审核的标志
                    IsAvailable = true,         //是否是可用的管理员
                    IsRoot = true               //注册企业号的人默认就是企业的超级管理员
                };
                //生成企业ID号
                user.CompanyId = Generate.GenerateCompanyId();
                string CompanyDbName = "Bonsaii" + user.CompanyId;
                user.ConnectionString = ConfigurationManager.AppSettings["UserDbConnectionString"] + CompanyDbName + ";" ;   //"Data Source = localhost,1433;Network Library = DBMSSOCN;Initial Catalog = " + CompanyDbName + ";User ID = test;Password = admin;";
                
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {

                    //添加注册的企业信息到Companies数据表当中
                    using (SystemDbContext sys = new SystemDbContext())
                    {
                        Company company = new Company()
                        {
                            CompanyId = user.CompanyId,
                            FullName = user.CompanyFullName,
                            TelNumber = user.UserName,
                            UserName = user.UserName
                        };
                        sys.Companies.Add(company);
                        sys.SaveChanges();
                    }
                    /**
                     * 注册成功并不会为企业创建独有的数据库,只有系统平台的超级管理员通过相应用户的审核之后才会为用户创建数据库
                     * */
                    //        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 
                    // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                    // 发送包含此链接的电子邮件
                    // 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, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");
                    return RedirectToAction("Login", "Account");
                }
                AddErrors(result);
            }
            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }
예제 #5
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            using (SystemDbContext con = new SystemDbContext())
            {
                ApplicationUser user = UserManager.FindByName(model.UserName);
                if (user == null)
                {
                    ModelState.AddModelError("", "用户名或密码错误!请重新登录");
                    return View(model);
                }
                if (!user.IsProved)
                {
                    ModelState.AddModelError("", "您所属的企业尚未通过审核。请与系统管理员联系。");
                    return View(model);
                }
                if (!user.IsAvailable)
                {
                    ModelState.AddModelError("", "抱歉,您已经被企业管理员禁用!请与企业管理员联系");
                    return View(model);
                }
            }

            // 这不会计入到为执行帐户锁定而统计的登录失败次数中
            // 若要在多次输入错误密码的情况下触发帐户锁定,请更改为 shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);     
            switch (result)
            {
                case SignInStatus.Success:
                    var user = UserManager.FindByName(model.UserName);
                    //设置session
                    Session["ConnectionString"] = user.ConnectionString;
                    Session["CompanyId"] = user.CompanyId;
                    Session["UserName"] = user.UserName;
                    Session["CompanyFullName"] = user.CompanyFullName;
                    Session["IsProved"] = user.IsProved;

                    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("", "无效的登录尝试。");
                    return View(model);
            }
        }
예제 #6
0
 public ActionResult ModifyPassword(ModifyPasswordViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (SystemDbContext con = new SystemDbContext())
     {
         ApplicationUser user = UserManager.FindByName(model.UserName);
         var result = UserManager.ChangePassword(user.Id, model.Password, model.NewPassword);
         if (result.Succeeded)
         {
             return RedirectToAction("ModifyPasswordConfirmation");
         }
         else
         {
             ModelState.AddModelError("", "用户名或密码错误!");
             return View(model);
         }
     }
 }
예제 #7
0
        public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                //首先核对用户的短信验证码是否合法
                using (SystemDbContext vCode = new SystemDbContext())
                {
                    var CurrentUserCode = vCode.VerifyCodes.Find(model.PhoneNumber);
                    DateTime CurTime = System.DateTime.Now;
                    if (CurTime > CurrentUserCode.OverTime)     //用户短信验证码超时
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码已经过期!");
                        return View(model);
                    }
                    else if (!CurrentUserCode.Code.Equals(model.Code))
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码输入错误!");
                        return View(model);
                    }
                }


                ApplicationUser user = UserManager.FindByName(model.PhoneNumber);
                var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var result = UserManager.ResetPassword(user.Id, code, model.Password);
                if (result.Succeeded)
                {
                    return RedirectToAction("ResetPasswordConfirmation");
                }


                // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                // 发送包含此链接的电子邮件
                // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);		
                // await UserManager.SendEmailAsync(user.Id, "重置密码", "请通过单击 <a href=\"" + callbackUrl + "\">此处</a>来重置你的密码");
                // return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }
예제 #8
0
 public ActionResult SwitchIsAvailable(string id)
 {
     using (SystemDbContext con = new SystemDbContext())
     {
         UserModels user = con.Users.Find(id);
         user.IsAvailable = user.IsAvailable ? false : true;
         con.SaveChanges();
     }
     return RedirectToAction("Index");
 }
예제 #9
0
        //
        // POST: /Roles/Create
        //[HttpPost]
        //public async Task<ActionResult> Create(RoleViewModel roleViewModel)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var role = new IdentityRole(roleViewModel.Name);
        //        var roleresult = await RoleManager.CreateAsync(role);
        //        if (!roleresult.Succeeded)
        //        {
        //            ModelState.AddModelError("", roleresult.Errors.First());
        //            return View();
        //        }
        //        return RedirectToAction("Index");
        //    }
        //    return View();
        //}

        //
        // POST: /Roles/Edit/5
        //[HttpPost]

        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> Edit([Bind(Include = "Name,Id")] RoleViewModel roleModel)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var role = await RoleManager.FindByIdAsync(roleModel.Id);
        //        role.Name = roleModel.Name;
        //        await RoleManager.UpdateAsync(role);
        //        return RedirectToAction("Index");
        //    }
        //    return View();
        //}

        //
        // GET: /Roles/Delete/5
        //public async Task<ActionResult> Delete(string id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    var role = await RoleManager.FindByIdAsync(id);
        //    if (role == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(role);
        //}

        ////
        //// POST: /Roles/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> DeleteConfirmed(string id, string deleteUser)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        if (id == null)
        //        {
        //            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //        }
        //        var role = await RoleManager.FindByIdAsync(id);
        //        if (role == null)
        //        {
        //            return HttpNotFound();
        //        }
        //        IdentityResult result;
        //        if (deleteUser != null)
        //        {
        //            result = await RoleManager.DeleteAsync(role);
        //        }
        //        else
        //        {
        //            result = await RoleManager.DeleteAsync(role);
        //        }
        //        if (!result.Succeeded)
        //        {
        //            ModelState.AddModelError("", result.Errors.First());
        //            return View();
        //        }
        //        return RedirectToAction("Index");
        //    }
        //    return View();
        //}

        public JsonResult GetAllActions()
        {
            SystemDbContext db = new SystemDbContext();
            List<Object> texts = new List<Object>();
            var ControllerNames = db.Actions.Select(p => p.ControllerName).Distinct();
            
            foreach (var name in ControllerNames)
            {
                SystemDbContext tmpDb = new SystemDbContext();
                //获取每一个Controller下面的所有Actoin
                var tmpActions = tmpDb.Actions.Where(p => p.ControllerName.Equals(name));
                List<Object> children = new List<Object>();
                foreach (var tmp in tmpActions)
                {
                    children.Add(new { text = tmp.ActionShowName, id = tmp.ActionId });
                }
                texts.Add(new { text = name, children = children });
            }
            return Json(texts);
            //return Json(new
            //{
            //    errorcode = "1"
            //});
        }