示例#1
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            app_user you = new app_user(txtUsername.Text);
            string   hsh = app_user.CreatePasswordHash(you.Salt, txtPassword.Text);

            //check password
            if (hsh == you.HashedPwd)
            {
                you.validLogin = true;
            }



            //check username is valid by checking if exception is thrown

            try
            {
                int fNameLength = you.FirstName.Length;
            }
            catch (NullReferenceException)
            {
                you.validLogin = false;
            }


            //use validLogin to create auth ticket

            if (you.validLogin)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, you.UserId.ToString(), DateTime.Now, DateTime.Now.AddMinutes(480), false, "Admin");


                //encrypt cookies
                string     encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie          = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                //add cookies
                Response.Cookies.Add(cookie);

                //create session variable
                Session["FullName"] = you.FirstName.ToString() + " " + you.LastName.ToString();

                //final redirect, well redirect to admin pages
                Response.Redirect("~/Home");
            }
        }
示例#2
0
 internal Fixture AddNameMapping(app_user app_user)
 {
     _nameMappings.Add(app_user);
     return(this);
 }
        public ActionResult Register(AuthenticationModel entity)
        {
            // Ensure we have a valid viewModel to work with
            if (!ModelState.IsValid)
            {
                return(View("Form", entity));
            }
            try
            {
                using (var db = new WebChatEntities())
                {
                    //Generate new id
                    Guid id = Guid.NewGuid();

                    /*
                     * Save username and password to app_user
                     */
                    string username = entity.Regesiter.Username.Trim().ToLower();
                    string password = entity.Regesiter.Password.Trim().ToLower();
                    //Hash password before save to database
                    string encrypt_password = BCrypt.Net.BCrypt.HashPassword(password);
                    var    loginInfo        = new app_user
                    {
                        app_user_id        = id,
                        username           = username,
                        encrypted_password = encrypt_password
                    };
                    db.app_user.Add(loginInfo);
                    //maybe check error here, method return 0 => no record added to database
                    db.SaveChanges();

                    /*
                     * Save customer info to customer table
                     */
                    string   email        = entity.Regesiter.Email.Trim().ToLower();
                    string   fullname     = entity.Regesiter.Fullname.Trim();
                    DateTime birth        = entity.Regesiter.Birth;
                    string   gender       = entity.Regesiter.Gender;
                    var      customerInfo = new customer();
                    customerInfo.app_user_id          = id;
                    customerInfo.fullname             = fullname;
                    customerInfo.status_online        = true;
                    customerInfo.last_online          = DateTime.Now;
                    customerInfo.email                = email;
                    customerInfo.gender               = gender.Equals("Male") ? true : false;
                    customerInfo.birth                = birth;
                    customerInfo.last_change_password = DateTime.Now;
                    db.customers.Add(customerInfo);
                    //maybe check error here, method return 0 => no record added to database
                    db.SaveChanges();

                    //Login with new account
                    FormsAuthentication.SignOut();
                    FormsAuthentication.SetAuthCookie(loginInfo.username, false);
                    Session["UserID"] = loginInfo.app_user_id;

                    //TODO add role customer
                    //TODO add notify for user to update information
                }
            }
            catch
            {
                throw;
            }

            return(RedirectToAction("Index", "WebChat"));
        }