Example #1
0
        public static User CreateUser(DBContext.User vmUser)
        {
            User newUser = new User();


            foreach (PropertyInfo key in newUser.GetType().GetProperties())
            {
                string propertyname = key.Name;

                if (vmUser.GetType().GetProperty(propertyname) != null)
                {
                    string propertyType = vmUser.GetType().GetProperty(propertyname).PropertyType.Name;

                    if (propertyType == "Boolean")
                    {
                        if (vmUser.GetType().GetProperty(propertyname).GetValue(vmUser) == null)
                        {
                            key.SetValue(newUser, false);
                        }
                        else
                        {
                            key.SetValue(newUser, vmUser.GetType().GetProperty(propertyname).GetValue(vmUser));
                        }
                    }
                    else if (propertyType == "Nullable`1")
                    {
                        if (vmUser.GetType().GetProperty(propertyname).GetValue(vmUser) == null)
                        {
                            key.SetValue(newUser, DateTime.Now);
                        }
                        else
                        {
                            key.SetValue(newUser, vmUser.GetType().GetProperty(propertyname).GetValue(vmUser));
                        }
                    }
                    else
                    {
                        if (vmUser.GetType().GetProperty(propertyname).GetValue(vmUser) == null)
                        {
                            key.SetValue(newUser, "");
                        }
                        else
                        {
                            key.SetValue(newUser, vmUser.GetType().GetProperty(propertyname).GetValue(vmUser));
                        }
                    }
                }
            }

            return(newUser);
        }
Example #2
0
 public IHttpActionResult Register(DBContext.User user)
 {
     try
     {
         userService.Create(user, user.Password);
         return(CreatedAtRoute("DefaultApi", new { id = user.Id }, user));
         //return Ok();
     }
     catch (AppException ex)
     {
         // return error message if there was an exception
         return(BadRequest(ex.Message));
     }
 }
Example #3
0
        public User Create(DBContext.User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (dbContext.Users.Any(x => x.UserName == user.UserName))
            {
                throw new AppException("UserName " + user.UserName + " is already taken");
            }

            //byte[] passwordHash, passwordSalt;
            //CreatePasswordHash(password, out passwordHash, out passwordSalt);

            //user.PasswordHash = passwordHash;
            //user.PasswordSalt = passwordSalt;

            dbContext.Users.Add(user);
            dbContext.SaveChanges();

            return(user);
        }
Example #4
0
        public ActionResult UserLogin(Models.Security.LoginModel vm)
        {
            bool isMailvalid = false;

            if (vm != null)
            {
                if (vm.loginID == null || vm.loginID.Length < 1 || vm.loginPassword == null || vm.loginPassword.Length < 1)
                {
                    ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                    TempData["loginError"] = loginError;
                    return(RedirectToAction("index", "Home"));
                }
                //since the user can connect using the email address and the id, we need to validate first if the email address is correct
                //if it is, then we search based on the email addreess, if not, we try to search based on the id, if the id is not valid also, then we failed and return tu user
                //validate the Email Address by using the MailAddress class from .Net Framework.
                //if the email Address received from the view is accurate, then it will create a instance of the MailAddress class. If not will fail
                //MailAddress mailAddress = new MailAddress(vm.loginID);
                //isMailvalid = (mailAddress.Address == vm.loginID);
                if (vm.loginID.Contains("@"))
                {
                    isMailvalid = true;
                }
                ;

                if (isMailvalid)
                {
                    if (ModelState.IsValid)
                    {
                        //try to get the details from the database
                        DBContext.User user = dbmodel.Users.Where(x => x.EmailAddress == vm.loginID && x.Password == vm.loginPassword).FirstOrDefault();
                        if (user != null)
                        {
                            //Get the settings for the useer
                            // Models.Security.UserSettings userSettings = new Models.Security.UserSettings(dbmodel.User_Settings.Where(x => x.ID == user.ID).FirstOrDefault());
                            Models.Security.User      userModel = Models.Security.User.CreateUser(user);
                            Models.Security.AuthState userState = new Models.Security.AuthState(userModel, vm.timezone);
                            userState.LogIn();
                            SetUserSession(userModel, userState);

                            return(RedirectToAction("Overview", "Portal"));
                        }
                        else
                        {
                            //Create Error Model
                            ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                            TempData["loginError"] = loginError;
                            return(RedirectToAction("index", "Home"));
                        }
                    }
                    //if the model is not valid, return back to user
                    else
                    {
                        ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("EmailIssue", true, "The Login Credential are not correct!");
                        TempData["loginError"] = loginError;
                        return(RedirectToAction("index", "Home"));
                    }
                }
                else //get user by the ID since the email failed.
                {
                    DBContext.User user = dbmodel.Users.Where(x => x.ID == vm.loginID && x.Password == vm.loginPassword).FirstOrDefault();
                    if (user != null)
                    {
                        //Get the settings for the useer
                        //Models.Security.UserSettings userSettings = new Models.Security.UserSettings(dbmodel.User_Settings.Where(x => x.ID == user.ID).FirstOrDefault());
                        Models.Security.User      userModel = Models.Security.User.CreateUser(user);
                        Models.Security.AuthState userState = new Models.Security.AuthState(userModel, vm.timezone);
                        userState.LogIn();

                        SetUserSession(userModel, userState);

                        return(RedirectToAction("Overview", "Portal"));
                    }
                    else
                    {
                        ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("IDIssue", true, "The Login Credential are not correct!");
                        TempData["loginError"] = loginError;
                        return(RedirectToAction("index", "Home"));
                    }
                }
            }
            else
            {
                ErrorManagement.LoginError loginError = ErrorManagement.LoginError.CreateModel("Datasend", true, "The Login Credential are not correct!");
                TempData["loginError"] = loginError;
                return(RedirectToAction("Error"));
            }
        }