Beispiel #1
0
        public IActionResult Index(LoginViewModel loginModel)
        {
            // Set Meta Data
            ViewData["Title"]              = "Sign In";
            ViewData["ContentKeywords"]    = functions.GetSiteLookupData("MetaKeywords");
            ViewData["ContentDescription"] = functions.GetSiteLookupData("MetaDescription");
            ViewData["PostAuthor"]         = "";

            if (ModelState.IsValid)
            {
                try
                {
                    // check password
                    var    query          = _context.Accounts.Where(s => s.Email == loginModel.Email);
                    string hashedPassword = (query.Any()) ? query.FirstOrDefault().Password : "";
                    if (!string.IsNullOrEmpty(hashedPassword))
                    {
                        //if login is valid
                        if (BCrypt.Net.BCrypt.Verify(loginModel.Password, hashedPassword))
                        {
                            //If account not activated
                            if (query.FirstOrDefault().Active == 0)
                            {
                                TempData["ErrorMessage"] = "Account not activated.";
                                return(View(loginModel));
                            }

                            //active account
                            else if (query.FirstOrDefault().Active == 1)
                            {
                                //Set sessions
                                _sessionManager.ID                 = query.FirstOrDefault().ID;
                                _sessionManager.LoginAccountId     = query.FirstOrDefault().AccountID;
                                _sessionManager.LoginUsername      = query.FirstOrDefault().Email.Split('@')[0];
                                _sessionManager.LoginEmail         = query.FirstOrDefault().Email;
                                _sessionManager.LoginFirstName     = (query.FirstOrDefault().FirstName != null) ? query.FirstOrDefault().FirstName : "";
                                _sessionManager.LoginLastName      = (query.FirstOrDefault().LastName != null) ? query.FirstOrDefault().LastName : "";
                                _sessionManager.LoginDirectoryName = query.FirstOrDefault().DirectoryName;
                                if (query.FirstOrDefault().Oauth == 0 && query.FirstOrDefault().Oauth != null)
                                {
                                    //set profile pic to default if null
                                    _sessionManager.LoginProfilePicture = (query.FirstOrDefault().FirstName != null) ? "/files/images/accounts/" + _sessionManager.LoginDirectoryName + "/" + query.FirstOrDefault().ProfilePicture : "/files/images/defaults/default-profile.jpg";
                                }
                                else
                                {
                                    _sessionManager.LoginProfilePicture = "/files/images/defaults/default-profile.jpg";
                                }

                                //log activity
                                if (_systemConfiguration.logActivity && _systemConfiguration.logSignIn)
                                {
                                    string LogAction = $@"User '{_sessionManager.LoginFirstName} {_sessionManager.LoginLastName}' logged in.";
                                    functions.LogActivity(_sessionManager.LoginAccountId, _sessionManager.LoginAccountId, "UserLogin", LogAction);
                                }

                                return(RedirectToAction("Index", "Admin"));
                            }
                            //Undefined status
                            else
                            {
                                TempData["ErrorMessage"] = "Account status unknown. Please contact admin.";
                                return(View(loginModel));
                            }
                        }
                        else
                        {
                            TempData["ErrorMessage"] = "Login failed. You have entered an invalid email or password";
                            return(View(loginModel));
                        }
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "Login failed. You have entered an invalid email or password";
                        return(View(loginModel));
                    }
                }
                catch (Exception ex)
                {
                    //Log Error
                    _logger.LogInformation("Login Error: " + ex.ToString());
                    TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email to " + functions.GetSiteLookupData("SupportEmail");
                    return(View(loginModel));
                }
            }
            TempData["ErrorMessage"] = "Login failed";
            return(View(loginModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Index(AccountsModel accountsModel)
        {
            // Set Meta Data
            ViewData["Title"]              = "Sign Up";
            ViewData["ContentKeywords"]    = functions.GetSiteLookupData("MetaKeywords");
            ViewData["ContentDescription"] = functions.GetSiteLookupData("MetaDescription");
            ViewData["PostAuthor"]         = "";

            if (ModelState.IsValid)
            {
                //verify password match
                string ConfirmPassword = Request.Form["ConfirmPassword"];
                if (!functions.PasswordsMatch(accountsModel.Password, ConfirmPassword))
                {
                    TempData["ErrorMessage"] = "Passwords do not match";
                    return(View(accountsModel));
                }

                //verify email does not exist
                if (_context.Accounts.Any(s => s.Email == accountsModel.Email))
                {
                    TempData["ErrorMessage"] = "Email already exists, please choose a different email";
                    return(View(accountsModel));
                }

                try
                {
                    //set registration default values
                    accountsModel.AccountID         = functions.GetGuid();
                    accountsModel.DirectoryName     = functions.GenerateDirectoryName(accountsModel.Email);
                    accountsModel.Active            = 0;
                    accountsModel.Oauth             = 0;
                    accountsModel.EmailVerification = 0;
                    accountsModel.UpdatedBy         = accountsModel.AccountID;
                    accountsModel.UpdateDate        = DateTime.Now;
                    accountsModel.DateAdded         = DateTime.Now;

                    //hashing password with BCrypt
                    accountsModel.Password = BCrypt.Net.BCrypt.HashPassword(accountsModel.Password);

                    _context.Add(accountsModel);
                    await _context.SaveChangesAsync();

                    //add account id to account details
                    if (!_context.AccountDetails.Any(s => s.AccountID == accountsModel.AccountID))
                    {
                        functions.AddTableData("AccountDetails", "AccountID", accountsModel.AccountID, _systemConfiguration.connectionString);
                    }

                    //send user email
                    //set email data
                    string   ToName            = functions.GetAccountData(accountsModel.AccountID, "FullName");
                    string[] MessageParagraphs = { "Hello " + ToName + ", ", "Thank you for registering to " + functions.GetSiteLookupData("SiteName") + ".", "Your registration would be reviewed by our team and you would be notified once approved.", "This may take up to 24 hours." };
                    string   PreHeader         = "New account registration notification.";
                    bool     Button            = false;
                    int      ButtonPosition    = 2;
                    string   ButtonLink        = null;
                    string   ButtonLinkText    = null;
                    string   Closure           = _systemConfiguration.emailClosure;
                    string   Company           = _systemConfiguration.emailCompany;
                    string   UnsubscribeLink   = _systemConfiguration.emailUnsubscribeLink;
                    string   MessageBody       = EmailFormating.FormatEmail(MessageParagraphs, PreHeader, Button, ButtonPosition, ButtonLink, ButtonLinkText, Closure, Company, UnsubscribeLink);

                    string FromEmail = _systemConfiguration.smtpEmail;
                    string ToEmail   = accountsModel.Email;
                    string Subject   = "Account Registration Email";

                    //Get smtp details
                    string smtpEmail   = _systemConfiguration.smtpEmail;
                    string smtpPass    = _systemConfiguration.smtpPass;
                    string displayName = _systemConfiguration.emailDisplayName;
                    string smtpHost    = _systemConfiguration.smtpHost;
                    int    smtpPort    = _systemConfiguration.smtpPort;

                    EmailService.SendEmail(FromEmail, ToEmail, Subject, MessageBody, smtpEmail, smtpPass, displayName, smtpHost, smtpPort);

                    //log activity
                    if (_systemConfiguration.logActivity)
                    {
                        string LogAction = $@"User '{ToName}' registered.";
                        functions.LogActivity(accountsModel.AccountID, accountsModel.AccountID, "NewRegistration", LogAction);
                    }


                    TempData["SuccessMessage"] = "Thank you for registering. Your registration would be reviewed by our team and you would be notified once approved. This may take up to 24 hours.";
                    return(RedirectToAction("Index", "SignIn"));
                }
                catch (Exception ex)
                {
                    //Log Error
                    _logger.LogInformation("Sign Up Error: " + ex.ToString());
                    TempData["ErrorMessage"] = "There was an error processing your request. Please try again. If this error persists, please send an email.";
                }
            }
            return(View(accountsModel));
        }