Beispiel #1
0
        /// <summary>
        /// Creates an instance of an Account for a UserId
        /// </summary>
        /// <param name="userId">UserId for whom to create an account for.</param>
        public Account(int userId)
        {
            Account account = new Account();
            // Give a UserId, to check whether there was any account found or not.
            account.UserId = 0;
            List<Account> accounts = new List<Account>();
            string accountsJson = File.ReadAllText(LoginService.AccountFile);
            accounts = JsonConvert.DeserializeObject<List<Account>>(accountsJson);

            foreach (Account acount in accounts)
            {
                // Get the account
                if (acount.UserId == userId)
                {
                    account = acount;
                }
            }
            if (account.UserId == 0)
            {
                throw new Exception("Sorry, no account was found with this UserId.");
            }

            // Set the properties!
            this.AccountLockedSince = account.AccountLockedSince;
            this.AuthenticationToken = account.AuthenticationToken;
            this.CreateDate = account.CreateDate;
            this.IsLocked = account.IsLocked;
            this.IsVerified = account.IsVerified;
            this.PasswordResetDate = account.PasswordResetDate;
            this.PasswordResetToken = account.PasswordResetToken;
            this.RequireEmailVerification = account.RequireEmailVerification;
            this.TokenExpires = account.TokenExpires;
            this.UserId = account.UserId;
        }
Beispiel #2
0
        /// <summary>
        /// Locks an account.
        /// </summary>
        /// <param name="account">Account to lock.</param>
        /// <returns></returns>
        public static bool LockAccount(Account account)
        {
            List<Account> accounts = new List<Account>();
            try
            {
                string accountsJson = File.ReadAllText(LoginService.AccountFile);
                accounts = JsonConvert.DeserializeObject<List<Account>>(accountsJson);

                foreach (Account acount in accounts)
                {
                    if (acount.UserId == account.UserId)
                    {
                        acount.IsLocked = true;
                    }
                }
                accountsJson = JsonConvert.SerializeObject(accounts);
                File.WriteAllText(LoginService.AccountFile, accountsJson);
                return true;
            }
            catch (Exception er)
            {
                throw new Exception(er.Message);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new account for a user.
        /// </summary>
        /// <param name="email">Email address to be assosiated with the user.</param>
        /// <param name="password">Password that user would use.</param>
        /// <returns></returns>
        public static bool CreateAccount(string email, string password)
        {
            // Start the Profile Creation Process.
            if (email == null || password == null)
            {
                throw new Exception("Profile's email address and/or password must never be null.");
                // return false;
            }
            else
            {
                try
                {
                    string hashedPassword = Crypto.SHA256(password);

                    if (File.Exists(AccountFile))
                    {
                        Account account = new Account() { Email = email, Password = Crypto.SHA256(password) };
                        string userListJson = File.ReadAllText(AccountFile);
                        List<Account> Accounts = JsonConvert.DeserializeObject<List<Account>>(userListJson)
                            .OrderByDescending(x => x.UserId)
                            .Reverse().ToList();

                        bool userExists = false;
                        long lastUser = 0;

                        // Check for current details..
                        foreach (Account acount in Accounts)
                        {
                            if (account.Email == email)
                            {
                                userExists = true;
                            }
                            lastUser = account.UserId;
                        }

                        account.UserId = lastUser + 1;

                        // If no user exists...
                        if (!userExists)
                        {
                            // Create a new user and append it to the list.
                            Accounts.Add(account);

                            string accountList = JsonConvert.SerializeObject(Accounts);
                            File.WriteAllText(LoginService.AccountFile, accountList);
                        }
                        else
                        {
                            throw new Exception("Profile Exists. Try another email address or login to continue.");
                        }

                        // save the list now..
                        

                        HttpCookie userCookie = new HttpCookie("createUserCookie");
                        userCookie["userId"] = account.UserId.ToString();
                        // It expires after 5 minutes.
                        userCookie.Expires.AddMinutes(5);

                        // Set the login cookie!
                        HttpContext.Current.Response.Cookies.Add(userCookie);

                        HttpCookie loginCookie = new HttpCookie("loginCookie");
                        loginCookie["userId"] = account.UserId.ToString();
                        // It expires after 5 minutes.
                        loginCookie.Expires.AddMinutes(60);

                        // Set the cookie!
                        HttpContext.Current.Response.Cookies.Add(loginCookie);

                        return true;
                    }
                    else
                    {
                        throw new FileNotFoundException("File not found at the location ~/App_Data/Admin/Profile.json, which is required for this login system to work. Please add the file at the location and make sure you allow Read/Write permissions.");
                    }
                }
                catch (Exception er)
                {
                    throw new Exception(er.Message);
                }
            }
        }