コード例 #1
0
        public async Task <IActionResult> Register(RegisterViewModel reg)
        {
            if (ModelState.IsValid)
            {
                //Check Username is not taken
                if (!await AccountDb.IsUserNameTaken(reg.UserName, _context))
                {
                    Account acc = new Account()
                    {
                        FullName = reg.FullName,
                        Email    = reg.Email,
                        Username = reg.UserName,
                        Password = reg.Password
                    };
                    //add account to database
                    await AccountDb.Register(acc, _context);

                    //create user session
                    SessionHelper.CreateUserSession(_http, acc.AccountID, acc.Username);
                    #region Manual CreateUserSession Practice
                    //HttpContext.Session.SetInt32("Id", acc.AccountID);
                    //HttpContext.Session.SetString("Username", acc.Username);
                    #endregion

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    //Display Error with other username error msg.
                    ModelState.AddModelError(nameof(Account.Username), "Username is already taken, Please pick another.");
                }
            }
            return(View(reg));
        }
コード例 #2
0
        public async Task <IActionResult> Register(RegisterViewModel reg)
        {
            if (ModelState.IsValid)
            {
                // Check username is not taken
                if (!await AccountDb.IsUsernameTaken(reg.Username, _context))
                {
                    Account acc = new Account()
                    {
                        Email    = reg.Email,
                        FullName = reg.FullName,
                        Password = reg.Password,
                        Username = reg.Username
                    };

                    await AccountDb.Register(_context, acc);

                    SessionHelper.CreateUserSession(acc.AccountId, acc.Username, _http);

                    return(RedirectToAction("Index", "Home"));
                }
                else // If username is taken, add error
                {
                    ModelState.AddModelError(nameof(Account.Username), "Username is already taken");
                }
            }

            return(View(reg));
        }
コード例 #3
0
        public async Task <IActionResult> Register(RegisterViewModel reg)
        {
            if (ModelState.IsValid)
            {
                //Check username is unique
                if (!await AccountDb.IsUsernameAvailable(reg.Username, _context))
                {
                    Account acc = new Account()
                    {
                        Email    = reg.Email,
                        FullName = reg.FullName,
                        Password = reg.Password,
                        Username = reg.Username
                    };

                    //if unique, add account to DB
                    await AccountDb.Register(_context, acc);

                    //with the DB made, this links the session CREATE USER SESSION
                    SessionHelper.CreateUserSession(acc.AccountId, acc.Username, _http);


                    return(RedirectToAction("Index", "Home"));
                }
                else // If username is taken, add error
                {
                    // Display error with other username error messages
                    //Tell it what class this property is in
                    ModelState.AddModelError(nameof(Account.Username)
                                             , "Username is already taken");
                }
            }

            return(View(reg));
        }