/// <summary>
        /// Initialise the users file in the blog location if there is not one there already
        /// giving the admin user the default password which will require changing first time it is used
        /// </summary>
        public override Boolean InitialiseUsers()
        {
            // Try and load the users to memory so they can be used by the provider
            try
            {
                users = LoadUsers();
            }
            catch
            {
                users = new BlogUsers();
            }

            // No users, set up default ones ..
            if (users.Logins.Count == 0)
            {
                // The user's file doesn't exist so create the base credentials that will need changing on first login
                users.Logins.Add(GenerateDefaultUser(BlogPermission.Admin)); // Use the default admin user generator

                // Save the base credentials to the file
                return(SaveUsers(users));
            }
            else
            {
                return(true);
            }
        }
示例#2
0
        public ActionResult EditProfile(BlogUsers user, HttpPostedFileBase ProfileImage)
        {
            ModelState.Remove("ModifiedUser");
            if (ModelState.IsValid)
            {
                if (ProfileImage != null && (ProfileImage.ContentType == "image/png" ||
                                             ProfileImage.ContentType == "image/jpg" ||
                                             ProfileImage.ContentType == "image/jpeg"))
                {
                    string filename = $"user_{user.Id}.{ProfileImage.ContentType.Split('/')[1]}";
                    ProfileImage.SaveAs(Server.MapPath($"~/images/{filename}"));
                    user.ProfileImage = filename;
                }

                BusinessResult <BlogUsers> _result = blogUserManager.UpdateProfile(user);

                if (_result.Errors.Count > 0)
                {
                    ModelState.AddModelError("", "Username or Email is already taken");
                    return(View(user));
                }

                Session["login"] = _result.Result;

                return(RedirectToAction("ShowProfile"));
            }
            return(View(user));
        }
示例#3
0
        public ActionResult DeleteConfirmed(int id)
        {
            BlogUsers blogUsers = blogUserManager.Find(x => x.Id == id);

            blogUserManager.Delete(blogUsers);
            return(RedirectToAction("Index"));
        }
        /// <summary>
        /// Initialise the memory provider with the default admin user always
        /// as there is no where to store the user it must be created each time
        /// </summary>
        /// <returns>Always return true</returns>
        public override Boolean InitialiseUsers()
        {
            // Try and load the users to memory so they can be used by the provider
            users = new BlogUsers();
            users.Logins.Add(GenerateDefaultUser(BlogPermission.Admin)); // Use the default admin user generator

            return(true);
        }
示例#5
0
        public ActionResult Login(BlogUsers user, bool RememberMe)
        {
            JSData json = new JSData();

            BLL.BlogUsersBLL userBLL    = new BLL.BlogUsersBLL();
            BlogUsers        userResult = userBLL.GetList(t => t.UserName == user.UserName).FirstOrDefault();

            if (userResult == null) //用户不存在
            {
                json.Message = "用户不存在!";
            }
            else if (userResult.UserPass == user.UserPass)     //登录成功
            {
                BLLSession.UserInfoSessioin = userResult;

                if (!string.IsNullOrEmpty(Request.QueryString["href"]))
                {
                    json.JSurl = Request.QueryString["href"];
                }
                else
                {
                    json.JSurl = "/";
                }
                if (RememberMe == true)
                {
                    HttpCookie cookie = CookieHelper.GetCookie("userInfo");
                    if (cookie == null)
                    {
                        cookie = new HttpCookie("userInfo");
                        cookie.Values.Add("userName", user.UserName);
                        cookie.Values.Add("userPass", user.UserPass);
                        cookie.Expires = DateTime.Now.AddMonths(6); //setting the valid time of the cookie  [6 months]
                        CookieHelper.AddCookie(cookie);
                    }
                    else
                    {
                        if (!cookie.Values["userName"].Equals(user.UserName))
                        {
                            CookieHelper.SetCookie("userInfo", "userName", user.UserName, DateTime.Now.AddMonths(6));
                        }
                        if (!cookie.Values["userPass"].Equals(user.UserPass))
                        {
                            CookieHelper.SetCookie("userInfo", "userPass", user.UserPass, DateTime.Now.AddMonths(6));
                        }
                    }
                }
                else
                {
                    CookieHelper.RemoveCookie("userInfo");
                }
            }
            else    //密码错误,登录失败
            {
                json.Message = "密码错误!";
            }
            return(Json(json));
        }
示例#6
0
 public string GetUsername()
 {
     if (HttpContext.Current.Session["login"] != null)
     {
         BlogUsers user = HttpContext.Current.Session["login"] as BlogUsers;
         return(user.Username);
     }
     return("system");
 }
示例#7
0
        public ActionResult EditProfile()
        {
            BlogUsers user = Session["login"] as BlogUsers;

            BusinessResult <BlogUsers> _result = blogUserManager.GetUser(user.Id);

            if (_result.Errors.Count > 0)
            {
                ModelState.AddModelError("", "Error");
            }

            return(View(_result.Result));
        }
示例#8
0
        // GET: BlogUser/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            BlogUsers blogUsers = blogUserManager.Find(x => x.Id == id.Value);

            if (blogUsers == null)
            {
                return(HttpNotFound());
            }
            return(View(blogUsers));
        }
示例#9
0
        public ActionResult DeleteProfile()
        {
            BlogUsers user = Session["login"] as BlogUsers;

            BusinessResult <BlogUsers> _result = blogUserManager.DeleteUser(user.Id);

            if (_result.Errors.Count > 0)
            {
                ModelState.AddModelError("", "Error");
                return(RedirectToAction("/Home/ShowProfile"));
            }

            Session.Clear();
            return(RedirectToAction("Index"));;
        }
示例#10
0
        public ActionResult Edit(BlogUsers blogUsers)
        {
            ModelState.Remove("ModifiedUser");
            ModelState.Remove("Created");
            ModelState.Remove("Modified");

            if (ModelState.IsValid)
            {
                BusinessResult <BlogUsers> _result = blogUserManager.Update(blogUsers);
                if (_result.Errors.Count > 0)
                {
                    ModelState.AddModelError("", "Error");
                    return(View(blogUsers));
                }
                return(RedirectToAction("Index"));
            }
            return(View(blogUsers));
        }
示例#11
0
        public ActionResult Create(BlogUsers blogUsers)
        {
            ModelState.Remove("ModifiedUser");
            ModelState.Remove("Created");
            ModelState.Remove("Modified");

            if (ModelState.IsValid)
            {
                BusinessResult <BlogUsers> _result = blogUserManager.Insert(blogUsers);
                if (_result.Errors.Count > 0)
                {
                    ModelState.AddModelError("", "Username or Email is already taken");
                    return(View(blogUsers));
                }
                return(RedirectToAction("Index"));
            }

            return(View(blogUsers));
        }
        /// <summary>
        /// Load a list of users from the XML Location
        /// </summary>
        /// <returns>A list of users</returns>
        private BlogUsers LoadUsers()
        {
            BlogUsers users = new BlogUsers(); // The default return parameter

            try
            {
                // Check to see if the file exists
                if (!File.Exists(UserFileLocation))
                {
                    users = Read <BlogUsers>(UserFileLocation);
                }
            }
            catch (Exception ex)
            {
                // Bounce up the chain the error about loading the user's list (and not because it wasn't there)
                throw BlogException.Passthrough(ex, new UserBlogException("Failed to connect to the users repository"));
            }

            return(users); // Return the list of users
        }
示例#13
0
        public ActionResult Register(BlogUsers user)
        {
            JSData       json       = new JSData();
            BlogTypesBLL typeBLL    = new BlogTypesBLL();
            BlogUsersBLL userBLL    = new BlogUsersBLL();
            BlogUsers    checkName  = userBLL.GetList(t => t.UserName == user.UserName).FirstOrDefault();
            BlogUsers    checkEmail = userBLL.GetList(t => t.Email == user.Email).FirstOrDefault();

            if (checkName != null)
            {
                json.State   = EnumState.失败;
                json.Message = "用户名已存在!";
            }
            else if (checkEmail != null)
            {
                json.State   = EnumState.失败;
                json.Message = "该邮箱已被注册!";
            }
            else
            {
                user.UserNickname = user.UserName;
                userBLL.Add(user);
                typeBLL.Add(new BlogTypes {
                    TypeName = "未分类", UserId = user.UserId, BlogUsers = user
                });
                try
                {
                    userBLL.save();
                    typeBLL.save();
                }catch (Exception ex)
                {
                    throw ex;
                }
                BLL.DataCache.GetUsersInfo(true);//更新缓存
                BLLSession.UserInfoSessioin = user;
                json.State   = EnumState.正常重定向;
                json.JSurl   = "/";
                json.Message = "恭喜你," + user.UserName + "注册成功!";
            }
            return(Json(json));
        }
        => throw new NotImplementedException();     // Not implemented in the base class

        /// <summary>
        /// Default Constructor
        /// </summary>
        public BlogDataProviderBase()
        {
            // Instantiate the items
            items = new BlogIndex();
            users = new BlogUsers();
        }
 /// <summary>
 /// Save a list of users to the XML location
 /// </summary>
 /// <param name="users"></param>
 /// <returns></returns>
 private Boolean SaveUsers(BlogUsers users)
 => Write <BlogUsers>(UserFileLocation, users);
示例#16
0
        protected override void Seed(DatabaseContext context)
        {
            BlogUsers blogUserAdmin = new BlogUsers()
            {
                Name         = "TestName",
                Surname      = "TestSurname",
                Username     = "******",
                Email        = "*****@*****.**",
                Password     = "******",
                ProfileImage = "profile.png",
                IsActive     = true,
                IsAdmin      = true,
                ActiveGuid   = Guid.NewGuid(),
                Created      = DateTime.Now,
                Modified     = DateTime.Now.AddHours(10),
                ModifiedUser = "******",
            };

            BlogUsers blogUserNormal = new BlogUsers()
            {
                Name         = "TestName 2",
                Surname      = "TestSurname 2",
                Username     = "******",
                Email        = "*****@*****.**",
                Password     = "******",
                ProfileImage = "profile.png",
                IsActive     = true,
                IsAdmin      = true,
                ActiveGuid   = Guid.NewGuid(),
                Created      = DateTime.Now,
                Modified     = DateTime.Now.AddHours(3),
                ModifiedUser = "******",
            };

            context.BlogUsers.Add(blogUserAdmin);
            context.BlogUsers.Add(blogUserNormal);

            for (int i = 0; i < 10; i++)
            {
                BlogUsers user = new BlogUsers()
                {
                    Name         = FakeData.NameData.GetFirstName(),
                    Surname      = FakeData.NameData.GetSurname(),
                    Username     = $"user{i}",
                    Email        = FakeData.NetworkData.GetEmail(),
                    Password     = "******",
                    ProfileImage = "profile.png",
                    IsActive     = true,
                    IsAdmin      = true,
                    ActiveGuid   = Guid.NewGuid(),
                    Created      = DateTime.Now,
                    Modified     = DateTime.Now.AddHours(3),
                    ModifiedUser = $"user{i}",
                };
                context.BlogUsers.Add(user);
            }

            context.SaveChanges();

            List <BlogUsers> userlist = context.BlogUsers.ToList();

            // category fakedata
            for (int i = 0; i < 10; i++)
            {
                Category category = new Category()
                {
                    Title        = FakeData.PlaceData.GetCountry(),
                    Description  = FakeData.PlaceData.GetAddress(),
                    Created      = DateTime.Now,
                    Modified     = DateTime.Now,
                    ModifiedUser = "******",
                };
                context.Categories.Add(category);

                // blog fakedata
                for (int k = 0; k < 5; k++)
                {
                    BlogUsers ownerFood = userlist[FakeData.NumberData.GetNumber(0, userlist.Count - 1)];

                    Food food = new Food()
                    {
                        Title        = FakeData.TextData.GetAlphabetical(FakeData.NumberData.GetNumber(10, 30)),
                        Text         = FakeData.TextData.GetSentences(FakeData.NumberData.GetNumber(1, 5)),
                        FoodImage    = "img_1.jpg",
                        IsDraft      = false,
                        LikeCount    = FakeData.NumberData.GetNumber(1, 10),
                        Owner        = ownerFood,
                        Created      = DateTime.Now,
                        Modified     = DateTime.Now.AddMinutes(10),
                        ModifiedUser = ownerFood.Username,
                    };
                    category.Foods.Add(food);

                    // comments fakedata
                    BlogUsers ownerComment = userlist[FakeData.NumberData.GetNumber(0, userlist.Count - 1)];

                    for (int j = 0; j < 4; j++)
                    {
                        Comment comment = new Comment()
                        {
                            Text         = FakeData.TextData.GetSentence(),
                            Owner        = ownerComment,
                            Created      = DateTime.Now,
                            Modified     = DateTime.Now.AddMinutes(5),
                            ModifiedUser = ownerComment.Username,
                        };
                        food.Comments.Add(comment);
                    }
                    // likes fakedata
                    for (int n = 0; n < food.LikeCount; n++)
                    {
                        Like likes = new Like()
                        {
                            LikedUsers = userlist[n]
                        };
                        food.Likes.Add(likes);
                    }
                }
            }
            context.SaveChanges();
        }