public bool DoesEmailExist(string email) { using (var dbc = new MiniBankEntities()) { var ExistingEmail = dbc.Users.Where(u => u.Email == email).FirstOrDefault(); return(ExistingEmail != null); } }
public List <Account> GetUserBankAccounts(int UserID) { List <Account> myAccounts = new List <Account>(); using (var dbc = new MiniBankEntities()) { myAccounts = dbc.Accounts.Where(a => a.CustomerID == UserID).ToList(); } return(myAccounts); }
public ActionResult Account(string id) { if (id != null) { Account account = null; int UserID = GetUserID(); using (var dbc = new MiniBankEntities()) { account = dbc.Accounts.Where(a => a.AccountNumber == id && a.CustomerID == UserID).FirstOrDefault(); if (account != null) { var startDate = DateTime.Now.AddMonths(-3); startDate = new DateTime(startDate.Year, startDate.Month, 1); var transactions = (from t in dbc.Bank_Transaction join tt in dbc.TransactionTypes on t.TransactionType equals tt.TypeID where t.AccountID == account.AccountID && t.TransactionDate >= startDate select new { t.TransactionID, t.TransactionDate, t.Reference, t.Amount, t.NewBalance, t.PreviousBalance, tt.TypeName, tt.IsDebit }).OrderByDescending(t => t.TransactionID).ToList(); var ParsedTransactionsList = new List <(int TranscationID, DateTime?TransactionDate, string Reference, decimal?Amount, decimal?NewBalance, decimal?PreviousBalance, string TypeName, bool IsDebit)>(); foreach (var t in transactions) { var transaction = ( t.TransactionID, t.TransactionDate, t.Reference, t.Amount, t.NewBalance, t.PreviousBalance, t.TypeName, t.IsDebit ); ParsedTransactionsList.Add(transaction); } ViewBag.MyTransactions = ParsedTransactionsList; return(View(account)); } else { return(RedirectToAction("Dashboard", "Home")); } } } else { return(RedirectToAction("Dashboard", "Home")); } }
// GET: Home public ActionResult Index() { List <(string AccountNumber, string AccountName, decimal?Amount, decimal?MonthOutgoing, decimal?MonthIncoming, decimal?MonthNet)> AccountsOverview = new List <(string AccountNumber, string AccountName, decimal?Amount, decimal?MonthOutgoing, decimal?MonthIncoming, decimal?MonthNet)>(); int UserID = GetUserID(); using (var dbc = new MiniBankEntities()) { string Name = dbc.Users.Where(u => u.UserID == UserID).Select(u => u.FirstName).FirstOrDefault(); ViewBag.UserName = Name; } using (var dbc = new MiniBankEntities()) { accounts = dbc.Accounts.Where(a => a.CustomerID == UserID).ToList(); ViewBag.OpenAccounts = accounts.Count(); var date = DateTime.Now; var startDate = new DateTime(date.Year, date.Month, 1); var endDate = startDate.AddMonths(1).AddDays(-1); foreach (var item in accounts) { var monthsOutgoing = dbc.Bank_Transaction .Join(dbc.TransactionTypes, bt => bt.TransactionType, t => t.TypeID, (bt, t) => new { bt, t }) .Where(btt => btt.bt.AccountID == item.AccountID && btt.t.IsDebit == true && (btt.bt.TransactionDate >= startDate && btt.bt.TransactionDate <= endDate)) .Sum(btt => btt.bt.Amount); var monthsIncoming = dbc.Bank_Transaction .Join(dbc.TransactionTypes, bt => bt.TransactionType, t => t.TypeID, (bt, t) => new { bt, t }) .Where(btt => btt.bt.AccountID == item.AccountID && btt.t.IsDebit == false && (btt.bt.TransactionDate >= startDate && btt.bt.TransactionDate <= endDate)) .Sum(btt => btt.bt.Amount); monthsOutgoing = monthsOutgoing == null ? 0 : monthsOutgoing; monthsIncoming = monthsIncoming == null ? 0 : monthsIncoming; var monthNet = monthsIncoming - monthsOutgoing; var accountInfo = ( item.AccountNumber, item.AccountName, item.Amount, monthsOutgoing, monthsIncoming, monthNet == null ? 0 : monthNet ); AccountsOverview.Add(accountInfo); } //Pay someone //Set up a direct debit } ViewBag.AccountsOverview = AccountsOverview; //ViewBag.pass = Crypto.Hash("password1234"); return(View()); }
public ActionResult Login(UserLogin login, string ReturnURL = "") { string message = ""; using (var dbc = new MiniBankEntities()) { var ExisitingUser = dbc.Users.Where(u => u.Email == login.Email).FirstOrDefault(); if (ExisitingUser != null) { if (string.Compare(ExisitingUser.Password, Crypto.Hash(login.Password)) == 0) { if (ExisitingUser.IsVerified == true) { int timeout = login.RememberMe ? 525600 : 30; var ticket = new FormsAuthenticationTicket(login.Email, login.RememberMe, timeout); string encrypted = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted); cookie.Expires = DateTime.Now.AddMinutes(timeout); cookie.HttpOnly = true; Response.Cookies.Add(cookie); if (Url.IsLocalUrl(ReturnURL)) { return(Redirect(ReturnURL)); } else { return(RedirectToAction("Index", "Home")); } } else { ViewBag.Warning = true; message = "This email address has not yet been verified, please check your inbox and junk folder."; } } else { message = "Invalid password and/or email provided"; } } else { message = "Invalid password and/or email provided"; } } ViewBag.Message = message; return(View()); }
public int GetUserID() { int id = 0; string email = User.Identity.Name; if (User.Identity.IsAuthenticated) { using (var dbc = new MiniBankEntities()) { id = dbc.Users.Where(u => u.Email == email).Select(u => u.UserID).FirstOrDefault(); } } return(id); }
public ActionResult ForgotPassword(AccountHelper helper) { bool status = false; string message = ""; User ExistingUser = null; //verify email exists using (var dbc = new MiniBankEntities()) { ExistingUser = dbc.Users.Where(u => u.Email == helper.Email).FirstOrDefault(); } if (ExistingUser != null) { //create new quick code var quickCode = new Random().Next(11111111, 99999999); var Token = Crypto.URLSafeHash(quickCode.ToString()); //create reset token in database using (var dbc = new MiniBankEntities()) { var newToken = new UserToken() { Token = Token, DateRequested = DateTime.Now, UserID = ExistingUser.UserID, Used = false }; dbc.UserTokens.Add(newToken); dbc.SaveChanges(); } //send email quick code email SendQuickCodeEmail(ExistingUser.Email, quickCode.ToString()); return(RedirectToAction("ForgotPassword", "User", new { id = Token })); } else { message = "We could not find this email address, please check your information and try again."; } ViewBag.Status = status; ViewBag.Message = message; return(View()); }
public ActionResult Dashboard() { int UserID = GetUserID(); using (var dbc = new MiniBankEntities()) { accounts = dbc.Accounts.Where(a => a.CustomerID == UserID).ToList(); ViewBag.MyAccounts = accounts; var transactions = (from t in dbc.Bank_Transaction join a in dbc.Accounts on t.AccountID equals a.AccountID join tt in dbc.TransactionTypes on t.TransactionType equals tt.TypeID where a.CustomerID == UserID select new { t.TransactionID, t.TransactionDate, t.Reference, t.Amount, t.NewBalance, t.PreviousBalance, a.AccountNumber, tt.TypeName, tt.IsDebit }).OrderByDescending(t => t.TransactionID).ToList(); var ParsedTransactionsList = new List <(int TranscationID, DateTime?TransactionDate, string Reference, decimal?Amount, decimal?NewBalance, decimal?PreviousBalance, string AccountNumber, string TypeName, bool IsDebit)>(); foreach (var t in transactions) { var transaction = ( t.TransactionID, t.TransactionDate, t.Reference, t.Amount, t.NewBalance, t.PreviousBalance, t.AccountNumber, t.TypeName, t.IsDebit ); ParsedTransactionsList.Add(transaction); } ViewBag.MyTransactions = ParsedTransactionsList; } return(View()); }
public ActionResult ForgotPassword(string id) { bool changePassword = false; if (id != null) { bool status = false; string message = ""; //verify email exists using (var dbc = new MiniBankEntities()) { //Validate token var tokenRecord = dbc.UserTokens.Where(ut => ut.Token == id).FirstOrDefault(); if (tokenRecord != null) { TimeSpan TimeSinceRequest = DateTime.Now.Subtract((DateTime)tokenRecord.DateRequested); if (TimeSinceRequest.TotalMinutes < 30) { ViewBag.Token = tokenRecord.Token; //Show form for validation code changePassword = true; ViewBag.ChangePassword = changePassword; ViewBag.Status = status; ViewBag.Message = message; return(View()); } else { return(RedirectToAction("ForgotPassword", "User")); } } else { return(RedirectToAction("ForgotPassword", "User")); } } } return(View()); }
public ActionResult OpenAccount(User user, string AccountName) { bool Status = false; string message = ""; int UserID = GetUserID(); if (AccountName != "") { using (var dbc = new MiniBankEntities()) { var userRecord = dbc.Users.Where(u => u.UserID == UserID).FirstOrDefault(); if (userRecord != null) { if (userRecord.Password == Crypto.Hash(user.Password)) { CreateBankAccount(UserID, AccountName); Status = true; message = "Congratulations your new account was successfully set up, and has been credited with £50 just for you!"; } else { message = "Authorisation failed, the password you entered did not match the account"; } } else { message = "Authorisation failed, your customer account was not found."; } } } else { message = "A valid account name was not supplied"; } ViewBag.Message = message; ViewBag.Status = Status; return(View()); }
public List <Account> GetPayeeBankAccounts(int UserID) { List <Account> myPayees = new List <Account>(); using (var dbc = new MiniBankEntities()) { var accounts = dbc.Payees.Where(p => p.PayeeUserID == UserID).Join(dbc.Accounts, p => p.PayeeAccountID, a => a.AccountID, (payee, account) => new { payee, account }).ToList(); foreach (var acc in accounts) { var payee = new Account() { AccountID = acc.account.AccountID, AccountName = acc.account.AccountName, AccountNumber = acc.account.AccountNumber }; myPayees.Add(payee); } } return(myPayees); }
public static void CreateBankAccount(int UserID, string AccountName) { using (var dbc = new MiniBankEntities()) { var newCustomer = dbc.Users.OrderByDescending(c => c.UserID).FirstOrDefault(); if (newCustomer != null) { var AccountToAdd = new Account { CustomerID = UserID, AccountNumber = new Random().Next(111111, 999999).ToString(), Amount = 50, AccountName = AccountName }; dbc.Accounts.Add(AccountToAdd); } dbc.SaveChanges(); } }
public ActionResult ResendActivation(AccountHelper helper) { bool status = false; string message = ""; //verify email exists using (var dbc = new MiniBankEntities()) { var EmailExists = dbc.Users.Where(u => u.Email == helper.Email).FirstOrDefault(); if (EmailExists != null) { //validate email has not already been activated if (!(bool)EmailExists.IsVerified) { //resend activation email SendVerificationLinkEmail(EmailExists.Email, EmailExists.ActivationCode.ToString(), true); status = true; message = $"Your activation email has been sent to {helper.Email}. Please remember to check your" + " junk mail folder, as it may be recieved there."; } else { message = "This email address has already been verified, you can <a href=\"/User/Login\" >login here</a>"; } } else { message = "We could not find this email address, please check your information and try again."; } } ViewBag.Status = status; ViewBag.Message = message; return(View()); }
public ActionResult VerifyAccount(string id) { bool Status = false; if (id != null) { using (var dbc = new MiniBankEntities()) { dbc.Configuration.ValidateOnSaveEnabled = false; //Find user with the matching activation code var userToValidate = dbc.Users.Where(u => u.ActivationCode == new Guid(id)).FirstOrDefault(); //if a user is found verify their account otherwise send error message to the user as invalid activation attempt if (userToValidate != null) { userToValidate.IsVerified = true; dbc.SaveChanges(); Status = true; } else { ViewBag.message = "Verification failed, the activation key does not exist"; } } } else { //If user somehow gets to this page directly, redirect them to the home page ViewBag.message = "Invalid Request"; return(RedirectToAction("Index", "Home")); } ViewBag.Status = Status; return(View()); }
public ActionResult Registration([Bind(Exclude = "IsVerified, ActivationCode")] User user) { bool Status = false; string message = ""; if (ModelState.IsValid) { #region Check If Email Exists var EmailExists = DoesEmailExist(user.Email); if (EmailExists) { ModelState.Remove("Email"); TempData["EmailExists"] = "This email address has already been registered"; ModelState.AddModelError("Email", "This email address has already been registered"); return(View()); } #endregion #region Generate Activation Code user.IsVerified = false; user.ActivationCode = Guid.NewGuid(); #endregion #region Password Hashing user.Password = Crypto.Hash(user.Password); user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword); #endregion #region Save to Database - Send Verification Email using (var dbc = new MiniBankEntities()) { //user.UserID = null; //Commit user to database dbc.Users.Add(user); dbc.SaveChanges(); SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString()); //Set success message for web output message = $"You have successfully registered to MiniBank!" + $" Welcome {user.FirstName}. An account verfication email" + $" has been sent to : {user.Email}"; Status = true; } var AccName = $"{user.FirstName.Substring(0, 1)}{user.LastName}"; AccountController.CreateBankAccount(user.UserID, AccName); #endregion } else { //Model error handling message = "Invalid Request"; } ViewBag.Message = message; ViewBag.Status = Status; return(View(user)); }
public ActionResult NewPayee(NewPayee payee) { bool status = false; string message = ""; Payee payeeToAdd = null; int UserID = GetUserID(); if (payee.Password != null) { using (var dbc = new MiniBankEntities()) { string HashedPass = Crypto.Hash(payee.Password); var verifiedUser = dbc.Users.Where(u => u.UserID == UserID && u.Password == HashedPass).FirstOrDefault(); if (verifiedUser == null) { message = "The password you entered was incorrect"; ViewBag.Status = status; ViewBag.Message = message; return(View()); } } } else { message = "The password you entered was incorrect"; ViewBag.Status = status; ViewBag.Message = message; return(View()); } //Validate Payee Info using (var dbc = new MiniBankEntities()) { var PayeeAccountDetails = dbc.Accounts.Where(a => a.AccountNumber == payee.PayeeAccount).FirstOrDefault(); if (PayeeAccountDetails != null) { var payeeExists = dbc.Payees.Where(p => p.PayeeAccountID == PayeeAccountDetails.AccountID && p.PayeeUserID == UserID).FirstOrDefault(); var myAccounts = GetUserBankAccounts(UserID); var IsMyAccount = myAccounts.Where(a => a.AccountID == PayeeAccountDetails.AccountID).FirstOrDefault(); if (IsMyAccount == null) { if (payeeExists == null) { payeeToAdd = new Payee(); payeeToAdd.PayeeAccountID = PayeeAccountDetails.AccountID; payeeToAdd.PayeeReference = payee.PayeeReference; payeeToAdd.DateCreated = DateTime.Now; payeeToAdd.PayeeUserID = UserID; dbc.Payees.Add(payeeToAdd); dbc.SaveChanges(); if (ModelState.IsValid) { ModelState.Clear(); } status = true; message = "Payee has successfully been added to your account"; } else { message = "This payee already exists"; } } else { message = "You cannot add one of your accounts as a payee, please head directly to <a href=\".. / Account / MoneyTransfer\">Money Transfer</a> to transfer money between your accounts"; } } else { message = "It was not possible to add your payee, please check the details you entered and try again."; } } ViewBag.Status = status; ViewBag.Message = message; //Add Payee return(View()); }
public ActionResult ForgotPasswordReset(AccountHelper helper, string ValidateToken) { //Verify entered token string message = ""; bool status = false; var userToken = Crypto.URLSafeHash(helper.QuickCode); if (userToken == ValidateToken) { if (helper.Password == helper.ConfirmPassword) { int UserToUpdate; using (var dbc = new MiniBankEntities()) { UserToUpdate = (int)dbc.UserTokens.Where(ut => ut.Token == userToken).Select(ut => ut.UserID).FirstOrDefault(); } if (UserToUpdate > 0) { using (var dbc = new MiniBankEntities()) { var user = dbc.Users.Find(UserToUpdate); if (user != null) { var newPassword = Crypto.Hash(helper.Password); user.Password = newPassword; user.ConfirmPassword = newPassword; dbc.SaveChanges(); status = true; message = "Your password has been sucessfully updated."; } else { message = "Password update failed, please try again"; } } } else { message = "Invalid Token"; } } else { message = "Passwords did not match"; } //redirect to change password page } else { message = "Invalid verification code"; } if (status) { using (var dbc = new MiniBankEntities()) { var tokentodiscard = dbc.UserTokens.Where(ut => ut.Token == userToken && ut.Used == false).FirstOrDefault(); if (tokentodiscard != null) { tokentodiscard.Used = true; dbc.SaveChanges(); } } } ViewBag.ChangePassword = true; ViewBag.Token = ValidateToken; ViewBag.Status = status; ViewBag.Message = message; return(View("ForgotPassword")); }
public ActionResult MoneyTransfer(TransactionData transaction) { bool Status = false; string message = ""; User userRecord; int UserID = GetUserID(); string FromAccount = transaction.FromAccountNumber; string ToAccount = transaction.ToAccountNumber; decimal TransactionValue = transaction.Amount; string Reference = transaction.Reference; string Password = transaction.Password; if (FromAccount != null && ToAccount != null && TransactionValue > 0 && Password != null) { using (var dbc = new MiniBankEntities()) { userRecord = dbc.Users.Where(u => u.UserID == UserID).FirstOrDefault(); } if (userRecord != null) { if (transaction.FromAccountNumber != transaction.ToAccountNumber) { if (userRecord.Password == Crypto.Hash(transaction.Password)) { string pattern = @"^[0-9]+(\.[0-9]{1,2})?$"; if (Regex.Match(transaction.Amount.ToString(), pattern).Success&& transaction.Amount > 0) { Account ConfirmAccountTo; Account ConfirmAccountFrom; using (var dbc = new MiniBankEntities()) { ConfirmAccountFrom = dbc.Accounts.Where(a => a.AccountNumber == transaction.FromAccountNumber.ToString()).FirstOrDefault(); ConfirmAccountTo = dbc.Accounts.Where(a => a.AccountNumber == transaction.ToAccountNumber.ToString()).FirstOrDefault(); if (ConfirmAccountTo != null && ConfirmAccountFrom != null) { if (ConfirmAccountFrom.Amount >= transaction.Amount) { Bank_Transaction transactionSend = new Bank_Transaction() { AccountID = ConfirmAccountFrom.AccountID, Amount = transaction.Amount, NewBalance = ConfirmAccountFrom.Amount - transaction.Amount, PreviousBalance = ConfirmAccountFrom.Amount, Reference = $"Ref Acc: {ConfirmAccountTo.AccountNumber}", TransactionType = 4, TransactionDate = DateTime.Now }; Bank_Transaction transactionRecieve = new Bank_Transaction() { AccountID = ConfirmAccountTo.AccountID, Amount = transaction.Amount, NewBalance = ConfirmAccountTo.Amount + transaction.Amount, PreviousBalance = ConfirmAccountTo.Amount, Reference = $"{transaction.Reference}", TransactionType = 3, TransactionDate = DateTime.Now }; dbc.Bank_Transaction.Add(transactionSend); dbc.Bank_Transaction.Add(transactionRecieve); dbc.SaveChanges(); //Transfer the money ConfirmAccountFrom.Amount -= transaction.Amount; ConfirmAccountTo.Amount += transaction.Amount; dbc.SaveChanges(); if (ModelState.IsValid) { ModelState.Clear(); } Status = true; ViewBag.Success = true; message = $"You successfully transfered {transaction.Amount} to Account {ConfirmAccountTo.AccountNumber}"; } else { message = "Sorry, you do not have sufficient funds to carry out this transaction"; } } else { message = "Account could not be found, please try again."; } } } else { message = "The amount you entered was not a valid monetary value"; } } else { message = "Authorisation failed, the password you entered did not match the account"; } } else { message = "Authorisation failed, you can not transfer money to the same account."; } } else { message = "Authorisation failed, your customer account was not found."; } } List <Account> myAccounts = GetUserBankAccounts(UserID); if (myAccounts.Count <= 0) { Status = false; } ViewBag.myAccounts = myAccounts; ViewBag.Message = message; ViewBag.Status = Status; return(View()); }