Ejemplo n.º 1
0
        public ActionResult Admin(AdminUserInfoModel model)
        {
            UserInfoModel userToUpdate = dbContext.Users.Find(model.UserInfoModel.Id);

            if (ModelState.IsValid)
            {
                try
                {
                    model.UserInfoModel.MembershipId = userToUpdate.MembershipId;
                    MembershipUser membershipUserToUpdate = Membership.GetUser(userToUpdate.MembershipId);
                    Roles.RemoveUserFromRoles(membershipUserToUpdate.UserName, Roles.GetRolesForUser(membershipUserToUpdate.UserName));
                    Roles.AddUserToRole(membershipUserToUpdate.UserName, model.Role);
                    if (model.UserInfoModel.IsActive == false)
                    {
                        DisableUser(membershipUserToUpdate);
                    }
                    else
                    {
                        EnableUser(membershipUserToUpdate);
                    }
                    dbContext.Entry(userToUpdate).CurrentValues.SetValues(model.UserInfoModel);
                    dbContext.SaveChanges();
                    TempData["isSuccessAdmin"] = true;
                    return(RedirectToAction("Admin"));
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Failed to update UserInfoModel", ex);
                }
            }
            GenerateRanksList(dbContext, ViewBag);
            GenerateRoleList();
            return(View(model));
        }
Ejemplo n.º 2
0
        public void SetLogin(AdminUserInfoModel adminUserInfo, bool isPersistent = false)
        {
            var now    = DateTime.Now;
            var ticket = new FormsAuthenticationTicket(
                version: 1,
                name: Guid.NewGuid().ToString("N"),
                issueDate: now,
                expiration: now.Add(_timeOut),
                isPersistent: isPersistent,
                userData: adminUserInfo.UserName,
                cookiePath: FormsAuthentication.FormsCookiePath);

            var cookie = new HttpCookie(_cookieIdStr, FormsAuthentication.Encrypt(ticket));

            cookie.HttpOnly = true;
            cookie.Secure   = FormsAuthentication.RequireSSL;
            cookie.Path     = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
            _loginAdminUser = adminUserInfo;
            try
            {
                _adminUserInfoService.SetLoginInfo(adminUserInfo.UserName, IpAddressHelper.GetIp());
            }
            catch
            {
            }
        }
Ejemplo n.º 3
0
        public ActionResult Admin(int id)
        {
            UserInfoModel userToAdmin = dbContext.Users.Find(id);

            GenerateRanksList(dbContext, ViewBag);
            GenerateRoleList();

            if (userToAdmin == null)
            {
                throw new InvalidOperationException("Could not find UserInfo for provided Id.");
            }

            MembershipUser membershipUser = Membership.GetUser(userToAdmin.MembershipId);

            if (membershipUser == null)
            {
                throw new InvalidOperationException("Could not find MembershipUser for provided MembershipId.");
            }

            AdminUserInfoModel model = new AdminUserInfoModel {
                Email = membershipUser.Email, UserName = membershipUser.UserName, Role = Roles.GetRolesForUser(membershipUser.UserName).Single()
            };

            model.UserInfoModel = userToAdmin;
            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult Add(AdminUserInfoModel paraModel)
        {
            var model = new BaseReturnModel()
            {
                IsSuccess = false, ReturnMsg = "操作失败"
            };

            var salt = StringHelper.GetSaltStr();

            var pwd = EncryptHelper.Md5(paraModel.Password, salt);

            try
            {
                _adminUserInfoService.Insert(new AdminUserInfo()
                {
                    UserName = paraModel.UserName,
                    Password = pwd,
                    PwdSalt  = salt,
                    CreateOn = DateTime.Now
                });
                model.IsSuccess = true;
                model.ReturnMsg = "添加完成";
            }
            catch (Exception ex)
            {
                model.IsSuccess = false;
                model.ReturnMsg = ex.Message;
            }
            return(Json(model));
        }
Ejemplo n.º 5
0
        public void SetLoginOut()
        {
            _loginAdminUser = null;
            var cookie = new HttpCookie(_cookieIdStr);

            cookie.Expires = DateTime.Now.AddSeconds(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        public void Update(AdminUserInfoModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            using (var db = new BaseDatabaseContext())
            {
                var oldEntity = db.AdminUserInfos.Find(model.Id);

                if (oldEntity == null)
                {
                    throw new Exception("oldEntity is null");
                }
                oldEntity = model.ToEntity(oldEntity);
                db.SaveChanges();
            }
        }
Ejemplo n.º 7
0
 public static AdminUserInfo ToEntity(this AdminUserInfoModel model, AdminUserInfo destination)
 {
     return(model.MapTo(destination));
 }
Ejemplo n.º 8
0
 public static AdminUserInfo ToEntity(this AdminUserInfoModel model)
 {
     return(model.MapTo <AdminUserInfoModel, AdminUserInfo>());
 }