public ActionResult RegisterMem(Member newMember, FormCollection form, string Account, string Password) { Acc_Pass newAccount = new Acc_Pass(); //random salt string salt = Guid.NewGuid().ToString(); DateTime dt = DateTime.Now; byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(Password + salt); byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes); string hashString = Convert.ToBase64String(hashBytes); newAccount.Account = Account; newAccount.Password = hashString; //pass.Account = Account; newAccount.Salt = salt; //pass.Password = hashString; string memId = db.Database.SqlQuery <string>("Select dbo.GetMemId()").FirstOrDefault(); newAccount.memId = memId; newMember.memId = memId; short county = Int16.Parse(form["County"].ToString()); short district = Int16.Parse(form["District"].ToString()); string sex = form["Sex"].ToString(); newMember.memCounty = county; newMember.memDistrict = district; newMember.timeReg = dt; newMember.Sex = sex; ViewBag.Account = Account; ViewBag.Password = Password; db.Member.Add(newMember); db.Acc_Pass.Add(newAccount); db.SaveChanges(); //try //{ // db.SaveChanges(); //} //catch (System.Data.Entity.Validation.DbEntityValidationException ex) //{ // throw ex; //} return(RedirectToAction("Index", "Activity")); }
public ActionResult Register(Member mem, string account, string password, string Introduction, string Habit, string Dietary_Preference) { //密碼雜湊 salt+hash string salt = Guid.NewGuid().ToString(); byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt); byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes); string hashString = Convert.ToBase64String(hashBytes); string getmmId = db.Database.SqlQuery <string>("select [dbo].[GetMemId]()").FirstOrDefault(); Acc_Pass acc = new Acc_Pass(); acc.memId = getmmId; acc.Account = account; acc.Password = hashString; acc.PasswordConfirm = hashString; mem.memCounty = Int16.Parse(Request["memCounty"]); mem.memDistrict = Int16.Parse(Request["memDistrict"]); mem.email_ID = Guid.NewGuid().ToString("N"); //型別轉換(string->char(1)) string gender = Request["Sex"]; if (gender == "男") { gender = "M"; } else { gender = "F"; } mem.Introduction = Introduction; mem.Habit = Habit; mem.Dietary_Preference = Dietary_Preference; mem.Sex = gender; mem.timeReg = DateTime.Now; mem.memId = getmmId; acc.Salt = salt; db.Acc_Pass.Add(acc); db.Member.Add(mem); db.SaveChanges(); MessageCenter mes = new MessageCenter(); List <string> mailList = new List <string>() { mem.Email }; mes.SendEmail(mailList, "JoinFun驗證信通知", "<img src='https://i.ibb.co/dcBqtJk/img.png' > <h3>親愛的" + acc.Account + "會員:</h3></br><h3>您在JoinFun的帳號已建立,請點擊下方連結以完成帳號啟用!</h3></br><a href='http://10.10.3.105/Register/Approved?email_ID=" + mem.email_ID + "'>信箱驗證請連結</a></br>"); return(RedirectToAction("CheckEmail", "Register", new { account = account })); }