public ActionResult SetRandenPassword(string Token = null) { if (string.IsNullOrWhiteSpace(Token)) { return(RedirectToAction("Index")); } var env = new Env(); var jws = new SimpleJws(); if (jws.Validate(Token, env.SecretKey)) { var payload = jws.Decode(Token); var MemberID = (string)payload["MemberID"]; var data = db.Members.Where(m => m.MemberID == MemberID).FirstOrDefault(); var password = ChoyPassword.Hash((string)payload["Password"], TimeConverter.ToTimestamp(data.CreateAt)); if (data.Psw != password) { try { data.Psw = password; db.Entry(data).State = EntityState.Modified; db.SaveChanges(); } catch { ViewBag.Error = "Sorry, the server is busy. Please try again later."; return(View()); } } } else { ViewBag.Error = "The apply has expired"; return(View()); } var url = Url.Action("Index") + "#/login"; return(Redirect(url)); }
public ActionResult _ChangePassword(string opsw, string psw2) { var session = ChoySession.Current; var MemberID = session.LoginId; var member = context.Members.Where(m => m.MemberID == MemberID).FirstOrDefault(); var currentpsw = opsw; var newpsw = psw2; long salt = TimeConverter.ToTimestamp(member.CreateAt); var now = DateTime.Now; if (ChoyPassword.Validate(currentpsw, salt, member.Psw)) { member.Psw = ChoyPassword.Hash(newpsw, salt); context.Entry(member).State = EntityState.Modified; context.SaveChanges(); } else { ViewBag.password = "******"; } return(RedirectToAction("EditMember")); }
protected override void Seed(ChoyContext context) { base.Seed(context); var now = DateTime.Now; List <Member> members = new List <Member> { new Member { MemberID = "M0001", Email = "*****@*****.**", Psw = ChoyPassword.Hash("000000", TimeConverter.ToTimestamp(now)), NickName = "Mei", Gender = false, Bday = new DateTime(1993, 05, 22), ContactEmail = "*****@*****.**", CreateAt = now, ProfilePic = getFileBytes("\\Images\\img4.jpg"), ImageMimeType = "image/jpeg", PerCode = Permissions.Download | Permissions.Bulletin | Permissions.Manager | Permissions.Suspension, //IsSuspended=false, //LastLogInTime=null }, new Member { MemberID = "M0002", Email = "*****@*****.**", Psw = ChoyPassword.Hash("000000", TimeConverter.ToTimestamp(now)), NickName = "Chun", Gender = false, Bday = new DateTime(1993, 06, 08), ContactEmail = "*****@*****.**", CreateAt = now, ProfilePic = getFileBytes("\\Images\\img4.jpg"), ImageMimeType = "image/jpeg", PerCode = Permissions.Download | Permissions.Manager | Permissions.Suspension, //IsSuspended=false, // LastLogInTime=null }, new Member { MemberID = "M0003", Email = "*****@*****.**", Psw = ChoyPassword.Hash("000000", TimeConverter.ToTimestamp(now)), NickName = "Fong", Gender = true, Bday = new DateTime(1993, 09, 20), ContactEmail = "*****@*****.**", CreateAt = now, ProfilePic = getFileBytes("\\Images\\img4.jpg"), ImageMimeType = "image/jpeg", PerCode = Permissions.Bulletin, IsSuspended = false, LastLogInTime = null } , new Member { MemberID = "M0004", Email = "*****@*****.**", Psw = ChoyPassword.Hash("000000", TimeConverter.ToTimestamp(now)), NickName = "Andy", Gender = true, Bday = new DateTime(1996, 12, 18), ContactEmail = "*****@*****.**", CreateAt = now, ProfilePic = getFileBytes("\\Images\\img4.jpg"), ImageMimeType = "image/jpeg", PerCode = Permissions.Download | Permissions.Manager | Permissions.Suspension, IsSuspended = false, LastLogInTime = null } }; members.ForEach(s => context.Members.Add(s)); context.SaveChanges(); List <Project> projects = new List <Project> { new Project { ProjectID = "P0001", ProjectName = "資料庫", CreateAt = DateTime.Now, DeleteAt = null, MemberID = "M0001" }, new Project { ProjectID = "P0002", ProjectName = "VueJS", CreateAt = DateTime.Now, DeleteAt = null, MemberID = "M0002" } }; projects.ForEach(s => context.Projects.Add(s)); context.SaveChanges(); List <Board> boards = new List <Board> { new Board { BoardID = "B0001", ProjectID = "P0001", MemberIDOwner = "M0001", DeleteAt = null }, new Board { BoardID = "B0002", ProjectID = "P0001", MemberIDOwner = "M0001", DeleteAt = null }, new Board { BoardID = "B0003", ProjectID = "P0001", MemberIDOwner = "M0001", DeleteAt = null } }; boards.ForEach(s => context.Boards.Add(s)); context.SaveChanges(); List <Bulletin> bulletins = new List <Bulletin> { new Bulletin { BulletinID = "N0001", PublishStart = DateTime.Now.AddDays(3), PublishEnd = DateTime.Now.AddDays(7), EditTime = DateTime.Now, Content = "Test1 " } }; bulletins.ForEach(s => context.Bulletins.Add(s)); context.SaveChanges(); // try // { // context.SaveChanges(); // } // catch (DbEntityValidationException ex) // { // var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); // var getFullMessage = string.Join("; ", entityError); // var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); // } List <Group> groups = new List <Group> { new Group { GroupID = "G0001", GroupName = "小圈圈", MemberID = "M0001" }, new Group { GroupID = "G0002", GroupName = "Java們", MemberID = "M0001" } , new Group { GroupID = "G0003", GroupName = "我的英文好夥伴", MemberID = "M0002" } , new Group { GroupID = "G0004", GroupName = "我的中文好夥伴", MemberID = "M0003" } }; groups.ForEach(s => context.Groups.Add(s)); context.SaveChanges(); List <GroupMember> groupMembers = new List <GroupMember> { new GroupMember { MemberIDOwner = "M0001", GroupID = "G0001", MemberIDInGroup = "M0002" }, new GroupMember { MemberIDOwner = "M0001", GroupID = "G0001", MemberIDInGroup = "M0003" } , new GroupMember { MemberIDOwner = "M0001", GroupID = "G0002", MemberIDInGroup = "M0003" } , new GroupMember { MemberIDOwner = "M0002", GroupID = "G0003", MemberIDInGroup = "M0001" } , new GroupMember { MemberIDOwner = "M0003", GroupID = "G0004", MemberIDInGroup = "M0001" } }; groupMembers.ForEach(s => context.GroupMembers.Add(s)); context.SaveChanges(); List <MemberJoinProject> memberJoinProjects = new List <MemberJoinProject> { new MemberJoinProject { MemberIDOwner = "M0001", ProjectID = "P0001", SharePerID = Share.Edit, MemberIDJoin = "M0002" }, new MemberJoinProject { MemberIDOwner = "M0001", ProjectID = "P0001", SharePerID = Share.Edit | Share.Share, MemberIDJoin = "M0003" } }; memberJoinProjects.ForEach(s => context.MemberJoinProjects.Add(s)); context.SaveChanges(); List <Vote> votes = new List <Vote> { new Vote { VoteID = 1, VoteName = "你今天早上要吃什麼?", Result = "日式拉麵", VoteCount = 2, ProjectID = "P0001", MemberIDOwner = "M0001" }, new Vote { VoteID = 2, VoteName = "你今天中午要吃什麼?", Result = "日式拉麵", VoteCount = 2, ProjectID = "P0001", MemberIDOwner = "M0001" }, }; votes.ForEach(s => context.Votes.Add(s)); context.SaveChanges(); List <VoteRecords> voteRecords = new List <VoteRecords> { new VoteRecords { VoteID = 1, ChoiceID = 1, Choice = "鍋燒意麵", VoteCounts = 1 }, new VoteRecords { VoteID = 1, ChoiceID = 2, Choice = "義大利麵", VoteCounts = 1 }, new VoteRecords { VoteID = 1, ChoiceID = 3, Choice = "日式拉麵", VoteCounts = 3 }, new VoteRecords { VoteID = 2, ChoiceID = 4, Choice = "鍋燒意麵", VoteCounts = 1 }, new VoteRecords { VoteID = 2, ChoiceID = 5, Choice = "日式拉麵", VoteCounts = 3 } }; voteRecords.ForEach(s => context.VoteRecords.Add(s)); context.SaveChanges(); }
public HttpResponseMessage Register(ApiAuthRegister data) { Env env = new Env(); SimpleJws jws = new SimpleJws(); HttpResponseMessage response = new HttpResponseMessage(); string token = data.Token; bool isSuccess = true; string messages = ""; if (token == null) // 缺乏 token { isSuccess = false; messages = "You don't have permission to access this server."; response.StatusCode = HttpStatusCode.Unauthorized; // 401 } else if (!jws.Validate(token, env.SecretKey)) // 缺乏 token { isSuccess = false; messages = "註冊連結已失效!!!"; response.StatusCode = HttpStatusCode.Unauthorized; // 401 } else if (data.Password.Length < 6 || data.Password.Length > 18) { isSuccess = false; messages = "Your password must be between 6 and 18 characters"; response.StatusCode = HttpStatusCode.BadRequest; // 400 } else { MemberSystem memberSystem = new MemberSystem(); Dictionary <string, object> jwt = jws.Decode(token); if (!jwt.ContainsKey("Email") || !jwt.ContainsKey("Gender") || !jwt.ContainsKey("Birthday")) { isSuccess = false; messages = "Invalid operation."; response.StatusCode = HttpStatusCode.BadRequest; // 400 } else { string name = jwt.ContainsKey("UserName") ? (string)jwt["UserName"] : null; DateTime now = DateTime.Now; Member member = new Member { Email = (string)jwt["Email"], Psw = ChoyPassword.Hash(data.Password, TimeConverter.ToTimestamp(now)), NickName = name, Gender = (bool)jwt["Gender"], Bday = TimeConverter.ToDateTime((long)jwt["Birthday"]), ContactEmail = (string)jwt["Email"], CreateAt = now, ProfilePic = memberSystem.GetFileBytes("\\Images\\carot.png"), ImageMimeType = "image/png", PerCode = 0, IsSuspended = false, LastLogInTime = now }; if (!memberSystem.Register(member)) { isSuccess = false; messages = "Registration failed"; response.StatusCode = HttpStatusCode.InternalServerError; // 500 } else { isSuccess = true; messages = "Registration success"; response.StatusCode = HttpStatusCode.OK; // 200 } } } var result = new { Success = isSuccess, Messages = messages }; response.Content = new StringContent(JsonConvert.SerializeObject(result)); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(response); }