//寄送驗證碼 public static void SendAuthCode(string Username) { string authcode = IDo.GetNewPW(); using (SqlConnection conn = new SqlConnection(sqlconn)) { string upacode = @"UPDATE Users SET AuthCode=@AuthCode WHERE Username = @Username"; using (SqlCommand cmd = new SqlCommand(upacode, conn)) { cmd.CommandType = CommandType.Text; SqlParameter pUsername = new SqlParameter("@Username", SqlDbType.NVarChar, 16); pUsername.Direction = ParameterDirection.Input; pUsername.Value = Username; cmd.Parameters.Add(pUsername); SqlParameter pAuthCode = new SqlParameter("@AuthCode", SqlDbType.NVarChar, 8); pAuthCode.Direction = ParameterDirection.Input; pAuthCode.Value = authcode; cmd.Parameters.Add(pAuthCode); conn.Open(); cmd.ExecuteNonQuery(); } string sqlcmd = @"SELECT @Email = Email FROM Users WHERE Username = @Username"; using (SqlCommand cmd = new SqlCommand(sqlcmd, conn)) { cmd.CommandType = CommandType.Text; SqlParameter pUser = new SqlParameter("@Username", SqlDbType.NVarChar, 16); pUser.Direction = ParameterDirection.Input; pUser.Value = Username; cmd.Parameters.Add(pUser); SqlParameter getEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 64); getEmail.Direction = ParameterDirection.Output; cmd.Parameters.Add(getEmail); cmd.ExecuteScalar(); string myEmail = getEmail.Value.ToString(); IDo.SendEmail(myEmail, Username, authcode); } } }
//寄送新密碼給使用者 public static bool CheckUser(string Username, string Email) { string sqlcmd = @"SELECT * FROM Users WHERE Username = @Username AND Email = @Email"; using (SqlConnection conn = new SqlConnection(sqlconn)) { using (SqlCommand cmd = new SqlCommand(sqlcmd, conn)) { cmd.CommandType = CommandType.Text; SqlParameter pUsername = new SqlParameter("@Username", SqlDbType.NVarChar, 16); pUsername.Direction = ParameterDirection.Input; pUsername.Value = Username; cmd.Parameters.Add(pUsername); SqlParameter pEmail = new SqlParameter("@Email", SqlDbType.NVarChar, 64); pEmail.Direction = ParameterDirection.Input; pEmail.Value = Email; cmd.Parameters.Add(pEmail); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { string NP = IDo.GetNewPW(); //創造新密碼 UpdatePW(Username, Email, NP); //更新密碼 IDo.SendEmail(Email, Username, NP); //寄email return(true); } else { return(false); } } } }