Example #1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password))
                {
                    using (MemberShipDataContext context = new MemberShipDataContext())
                    {
                        UserProfile usr = context.UserProfiles.Where(c => c.UserName.Equals(model.UserName)).FirstOrDefault();
                        if (null != usr)
                        {
                            HttpCookie cookie = new HttpCookie("User");
                            cookie.Values.Add("RealName", HttpUtility.UrlEncode(usr.RealName, System.Text.Encoding.UTF8));
                            cookie.Values.Add("ID", HttpUtility.UrlEncode(usr.ID.ToString(), System.Text.Encoding.UTF8));
                            cookie.Values.Add("UnitNum", HttpUtility.UrlEncode(usr.UnitNum.ToString(), System.Text.Encoding.UTF8));

                            cookie.Expires.AddHours(3);
                            Response.Cookies.Add(cookie);
                            using (HelperDataContext onlinecontext = new HelperDataContext())
                            {
                                UserAction newaction = new UserAction();
                                newaction.ID = usr.ID;
                                newaction.RealName = usr.RealName;
                                newaction.ActionName = "Login";
                                newaction.CreateTime = DateTime.Now;
                                newaction.LastModifyTime = newaction.CreateTime;
                                UserAction action = onlinecontext.UserActions.Where(c => c.ActionName.Equals(newaction.ActionName) && c.ID.Equals(newaction.ID) && c.CreateTime.Year.Equals(newaction.CreateTime.Year) && c.CreateTime.Month.Equals(newaction.CreateTime.Month) && c.CreateTime.Day.Equals(newaction.CreateTime.Day)).FirstOrDefault();
                                if (null == action)
                                {
                                    onlinecontext.UserActions.Add(newaction);
                                }
                                else
                                {
                                    action.LastModifyTime = newaction.LastModifyTime;
                                }
                                onlinecontext.SaveChanges();
                            }
                        }
                        else
                        {
                            WebSecurity.Logout();
                            return RedirectToAction("Login");
                        }
                        return RedirectToAction("Front", "Admin");
                    }
                }
                ModelState.AddModelError("", "用户名或密码错误");
                return View(model);
            }
            catch
            {
                WebSecurity.Logout();
                return RedirectToAction("Login", "Admin");
            }
        }
Example #2
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password))
                {
                    using (MemberShipDataContext context = new MemberShipDataContext())
                    {
                        UserProfile usr = context.UserProfiles.Where(c => c.UserName.Equals(model.UserName)).FirstOrDefault();
                        if (null != usr)
                        {
                            HttpCookie cookie = new HttpCookie("User");
                            cookie.Values.Add("RealName", HttpUtility.UrlEncode(usr.RealName, System.Text.Encoding.UTF8));
                            cookie.Values.Add("ID", HttpUtility.UrlEncode(usr.ID.ToString(), System.Text.Encoding.UTF8));

                            using (InfoManageDataContext infocontext = new InfoManageDataContext())
                            {
                                if (Roles.IsUserInRole(usr.UserName, AllConsts.adminRole))
                                {
                                    Response.Cookies.Add(cookie);
                                    return RedirectToAction("Front", "ManageAdmin");
                                }
                                else
                                {
                                    StoragesInfo info = infocontext.StoragesInfos.Where(c => c.StoragesInfoID.Equals(usr.UnitNum)).FirstOrDefault();
                                    cookie.Values.Add("UnitNum", HttpUtility.UrlEncode(usr.UnitNum.ToString(), System.Text.Encoding.UTF8));
                                    cookie.Values.Add("UnitName", HttpUtility.UrlEncode(info.StoragesInfoName, System.Text.Encoding.UTF8));

                                    cookie.Expires.AddHours(3);
                                    Response.Cookies.Add(cookie);
                                    using (InfoManageDataContext onlinecontext = new InfoManageDataContext())
                                    {
                                        UserAction newaction = new UserAction();
                                        newaction.ID = usr.ID;
                                        newaction.RealName = usr.RealName;
                                        newaction.ActionName = "Login";
                                        newaction.CreateTime = DateTime.Now;
                                        newaction.LastModifyTime = newaction.CreateTime;
                                        onlinecontext.UserActions.Add(newaction);
                                        onlinecontext.SaveChanges();
                                    }
                                }
                            }
                        }
                        else
                        {
                            WebSecurity.Logout();
                            return RedirectToAction("Login");
                        }
                        return RedirectToAction("Front", "Admin");
                    }
                }
                ModelState.AddModelError("", "用户名或密码错误");
                return View(model);
            }
            catch
            {
                WebSecurity.Logout();
                return RedirectToAction("Login", "Admin");
            }
        }