Esempio n. 1
0
        private void CreateUser()
        {
            var user = new User();

            user.Username = "******";
            user.Password = _secure.EncryptData("test");
            _conn.InsertUser(user);
            _id = _conn.GetUserByName(user.Username).UserId;
        }
Esempio n. 2
0
        private void SetConfig()
        {
            Simple3Des jm = new Simple3Des("deny01.12345678901234567890zb");

            Mydataconn mc = Mydataconn.Load();

            if (null == mc)
            {
                mc = new Mydataconn();
            }

            mc.Sqlser     = this.textBoxSQLSERVER.Text;
            mc.Databs     = this.textDatabaseName.Text;
            mc.SqlserUser = this.textBoxSQLuser.Text;
            mc.ServerKey  = jm.EncryptData(this.textBoxSQLPW.Text);

            mc.UserID = DataHelper.ToDBC(this.textBoxuserID.Text);
            mc.Passwd = jm.EncryptData(this.textBoxuserPW.Text);



            mc.Save();
        }
Esempio n. 3
0
 async void createButton_Clicked(object sender, EventArgs e)
 {
     if (_nameCheck && _passCheck)
     {
         var user    = new User();
         var newPass = _secure.EncryptData(passEntry.Text);
         user.Username = nameEntry.Text;
         user.Password = newPass;
         _conn.InsertUser(user);
         await Navigation.PushAsync(new LoginPage(), true);
     }
     else
     {
         await DisplayAlert("Alert", "Please Enter a Valid Username and Password", "OK");
     }
 }
Esempio n. 4
0
 private void cmdEncode_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(txtString.Text.Trim()))
     {
         string CryptKey    = txtKey.Text.Trim();
         string CryptString = txtString.Text.Trim();
         using (Simple3Des wrapper = new Simple3Des(CryptKey))
         {
             txtResult.Text = wrapper.EncryptData(CryptString);
         }
     }
     else if (!string.IsNullOrEmpty(txtResult.Text.Trim()))
     {
         using (Simple3Des wrapper = new Simple3Des(txtKey.Text.Trim()))
         {
             txtString.Text = wrapper.DecryptData(txtResult.Text.Trim());
         }
     }
 }
Esempio n. 5
0
        private void button3_Click(object sender, EventArgs e)
        {
            System.Management.ManagementClass mc  = new ManagementClass("win32_processor");
            ManagementObjectCollection        moc = mc.GetInstances();

            this.textBox1.Text = "";
            string cpuId = "";

            //this.textBox2.Text = "";

            foreach (ManagementObject mo in moc)
            {
                //MessageBox.Show(mo["processorid"].ToString());
                cpuId = cpuId + mo["processorid"].ToString();
            }

            Simple3Des jm = new Simple3Des(this.textBox2.Text);

            this.textBox1.Text = jm.EncryptData(cpuId);
            this.textBox3.Text = cpuId;
        }
Esempio n. 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            Simple3Des jm = new Simple3Des("deny01.12345678901234567890zb");

            System.Management.ManagementClass mcpu = new ManagementClass("win32_processor");
            ManagementObjectCollection        moc  = mcpu.GetInstances();

            string scpuCode = "";

            foreach (ManagementObject mo in moc)
            {
                //MessageBox.Show(mo["processorid"].ToString());
                scpuCode = scpuCode + mo["processorid"].ToString();
            }


            if (jm.EncryptData(scpuCode) == this.textBox1.Text)
            {
                Mydataconn mc = Mydataconn.Load();

                if (null == mc)
                {
                    mc = new Mydataconn();
                }

                mc.RegisterCode = this.textBox1.Text;

                mc.Save();

                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("注册码错误,请重新输入...");
            }
        }
Esempio n. 7
0
        //[Produces("application/json")]
        public async Task <string> Post(/*[FromForm] dynamic value, */ [FromRoute] string subRoute, List <IFormFile> files = null)
        {
            string[] checker; Users tUser;// Users[] lUser;
            switch (subRoute.ToLower())
            {
            case "auth": {
                checker = new string[] { UID, PWD };
                if (!CheckPayLoad(checker))
                {
                    return(ErrorInvalid());
                }
                if (!ModelState.IsValid)
                {
                    return(ErrorBadReq());
                }
                tUser = await db.users.SingleOrDefaultAsync(user => user.Username == Request.Form[UID][0]);

                if (tUser == null)
                {
                    return(ErrorUserNotFound());
                }
                Simple3Des des = new Simple3Des(Request.Form[UID][0]);
                if (tUser.Password == des.EncryptData(Request.Form[PWD][0]))
                {
                    if (tUser.Status == false)
                    {
                        return(Send(new { status = "error", desc =
                                              "Your account have been suspended. Due to some reason you are not eligible to use this account.<br>" +
                                              "For more information, please ask your administrator." }));
                    }
                    SetAuth(tUser.ID);
                    Console.WriteLine(tUser.Username + "[" + tUser.ID + "] Logged in");
                    logger.Write(LOG_MODE.ACTION, "ID " + GetID() + " login");
                    return(Send(new {
                            status = "success"
                        }));
                }
                else
                {
                    return(ErrorWrongPwd());      //Send(new { aa = des.EncryptData(Request.Form[PWD][0]), bb = tUser.UserPassword, cc = tUser.UserId });
                }
            }

            case "datalist": {
                checker = new string[] { DT_SKIP, DT_TAKE, DT_REQTOTAL };
                if (!CheckAccess(ACCESS.MASTER))
                {
                    return(ErrorAccess());
                }
                if (!ModelState.IsValid)
                {
                    return(ErrorBadReq());
                }
                if (!CheckPayLoad(checker))
                {
                    if (!CheckPayLoad(new string[] { DT_REQTOTAL, "sort" }))
                    {
                        return(ErrorInvalid());
                    }
                    return(false.ToString());
                }
                string sortBy = "ID", search = ""; bool bDesc = false;
                //var q = db.users.FromSqlRaw("select * from Users");
                //var g = db.groups.AsEnumerable();
                var query = db.users.Join(db.groups.AsEnumerable(), user => user.ID_Group, group => group.ID,
                                          (user, group) => new {
                        ID           = user.ID, Username = user.Username, Role = group.Name,
                        Email        = user.Email, Status = user.Status, IdGroup = user.ID_Group,
                        CustomAccess = user.Access
                    }
                                          ).AsEnumerable();

                /*var query = db.users.Select(user => new {
                 *  user.ID, user.Username, user.Role,
                 *  user.Email, user.Status, IdGroup = user.ID_Group,
                 *  CustomAccess = user.Access
                 * }).AsEnumerable();*/
                //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(query.ToList()));
                if (CheckPayLoad(new string[] { "filter" }))
                {
                    var filter = JsonConvert.DeserializeObject <JArray>(Request.Form["filter"]);
                    if ((string)filter[0][0] == "Username" && (string)filter[0][1] == "contains")
                    {
                        search = filter[0][2].ToString().ToLower();
                        query  = query.Where(user =>
                                             //EF.Functions.Like(user.Role, "%" + search + "%") ||
                                             user.Role.ToLower().Contains(search) ||
                                             user.Email.ToLower().Contains(search) ||
                                             user.Username.ToLower().Contains(search)
                                             );
                    }
                    else if ((string)filter[0][0] == "ID" && (string)filter[0][1] == "=")
                    {
                        search = filter[0][2].ToString().ToLower();
                        query  = query.Where(user => user.ID.ToString().Contains(search) ||
                                             //EF.Functions.Like(user.Role, "%" + search + "%") ||
                                             user.Role.ToLower().Contains(search) ||
                                             user.Email.ToLower().Contains(search) ||
                                             user.Username.ToLower().Contains(search)
                                             );
                    }
                }
                if (CheckPayLoad(new string[] { "sort" }))
                {
                    var sort = JsonConvert.DeserializeObject <JArray>(Request.Form["sort"]);
                    sortBy = (string)sort[0]["selector"]; bDesc = (bool)sort[0]["desc"];
                    if (bDesc == false)
                    {
                        switch (sortBy.ToLower())
                        {
                        case "id": query = query.OrderBy(ob => ob.ID); break;

                        case "username": query = query.OrderBy(ob => ob.Username); break;

                        case "role": query = query.OrderBy(ob => ob.Role); break;

                        case "email": query = query.OrderBy(ob => ob.Email); break;

                        case "status": query = query.OrderBy(ob => ob.Status); break;
                        }
                        //query.OrderBy(ob => EF.Property<object>(ob, sortBy));
                    }
                    else
                    {
                        switch (sortBy.ToLower())
                        {
                        case "id": query = query.OrderByDescending(ob => ob.ID); break;

                        case "username": query = query.OrderByDescending(ob => ob.Username); break;

                        case "role": query = query.OrderByDescending(ob => ob.Role); break;

                        case "email": query = query.OrderByDescending(ob => ob.Email); break;

                        case "status": query = query.OrderByDescending(ob => ob.Status); break;
                        }
                    }
                }
                query = query.Skip(Convert.ToInt32(Request.Form[DT_SKIP][0]));
                query = query.Take(Convert.ToInt32(Request.Form[DT_TAKE][0]));
                var data = query.ToList();

                if (data.Count() < 1)
                {
                    return("[]");
                }
                int totalCount = await db.users.CountAsync();

                var result = new { data, totalCount = totalCount };
                return(Send(result));
            }

            case "new": {
                try {
                    checker = new string[] {
                        "uid", "pwdNew", "email" /*, "status", "role"*/, "customAccess"
                    };
                    if (!CheckPayLoad(checker))
                    {
                        return(ErrorInvalid());
                    }
                    if (!CheckAccess(ACCESS.USER_ADD))
                    {
                        return(ErrorAccess());
                    }
                    if (!ModelState.IsValid)
                    {
                        return(ErrorBadReq());
                    }
                    var checkUser = await db.users.SingleOrDefaultAsync(user => user.Username == (string)Request.Form["uid"]);

                    if (checkUser != null)
                    {
                        return(ErrorUserExist());
                    }
                    Simple3Des des = new Simple3Des(Request.Form["uid"]);
                    //Console.WriteLine(Send(Request.Form));
                    var result = await db.users.AddAsync(new Users {
                            Username = (string)Request.Form["uid"],
                            ID_Group = Request.Form["role"] == "null" || Request.Form["role"] == "undefined" ? 0: Convert.ToInt32(Request.Form["role"]),
                            Status   = Convert.ToBoolean(Request.Form["status"]),
                            Email    = (string)Request.Form["email"], Access = (string)Request.Form["customAccess"],
                            Password = des.EncryptData((string)Request.Form["pwdNew"]), Logged_In = false
                        });

                    if (result.State == EntityState.Added)
                    {
                        result.Context.SaveChanges();
                    }
                    else if (result.State == EntityState.Unchanged)
                    {
                        return(Send(new {
                                status = "error", desc = "Can't add new User, due to the state is remainly unchanged"
                            }));
                    }
                    if (files.Count == 1)
                    {
                        var   fStream = new FileStream(hosting.WebRootPath + "\\images\\profiles\\" + result.Entity.ID + ".jpg", FileMode.Create);
                        await files[0].CopyToAsync(fStream);
                        fStream.Dispose(); fStream = null;
                    }
                    else
                    {
                        if (System.IO.File.Exists(hosting.WebRootPath +
                                                  "\\images\\profiles\\" + result.Entity.ID + ".jpg"))
                        {
                            System.IO.File.Delete(hosting.WebRootPath +
                                                  "\\images\\profiles\\" + result.Entity.ID + ".jpg");
                        }
                    }
                    logger.Write(LOG_MODE.ACTION, "ID " + GetID() + " added new user(" + result.Entity.ID + ")");
                } catch (Exception exc) {
                    return(Send(new {
                            status = "error", desc = exc.Message, number = exc.HResult,
                            source = exc.Source, sTrace = exc.StackTrace
                        }));
                }
                return(Send(new {
                        status = "success"
                    }));
            }

            case "update": {
                try {
                    checker = new string[] {
                        "id", "uid", "pwdNew", "pwdOld", "email", "status", "role", "customAccess",
                        "clearPhoto"
                    };
                    if (!CheckPayLoad(checker))
                    {
                        return(ErrorInvalid());
                    }
                    if (!CheckAccess(ACCESS.USER_EDIT))
                    {
                        return(ErrorAccess());
                    }
                    if (!ModelState.IsValid)
                    {
                        return(ErrorBadReq());
                    }

                    var cekUser = await db.users.SingleOrDefaultAsync(user => user.ID == Convert.ToInt32(Request.Form["id"]));

                    if (cekUser == null)
                    {
                        return(ErrorUserNotFound());
                    }
                    Simple3Des des = new Simple3Des(cekUser.Username);
                    if (cekUser.Password != des.EncryptData(Request.Form["pwdOld"]))
                    {
                        return(Send(new { status = "error", desc = "Old password didn't match" }));
                    }
                    if (cekUser.Username != Request.Form["uid"])
                    {
                        var cekUserExist = await db.users.SingleOrDefaultAsync(user =>
                                                                               user.Username == Request.Form["uid"]);

                        if (cekUserExist != null)
                        {
                            return(ErrorUserExist());
                        }
                    }
                    des = new Simple3Des(Request.Form["uid"]);
                    cekUser.Username = Request.Form["uid"];
                    cekUser.Password = des.EncryptData(Request.Form["pwdNew"]);
                    cekUser.Email    = Request.Form["email"];
                    cekUser.Status   = Convert.ToBoolean(Request.Form["status"]);
                    cekUser.ID_Group = Request.Form["role"] == "null" || Request.Form["role"] == "undefined" ? 0 : Convert.ToInt32(Request.Form["role"]);
                    cekUser.Access   = (string)Request.Form["customAccess"] == "null" ? "" : (string)Request.Form["customAccess"];
                    var result = db.users.Update(cekUser);
                    if (result.State == EntityState.Modified)
                    {
                        await result.Context.SaveChangesAsync();
                    }
                    else if (result.State == EntityState.Unchanged)
                    {
                        return(Send(new {
                                status = "error", desc = "Can't edit existing User, due to state is remainly unchanged"
                            }));
                    }
                    if (Request.Form["clearPhoto"] == "true")
                    {
                        if (System.IO.File.Exists(hosting.WebRootPath +
                                                  "\\images\\profiles\\" + cekUser.ID + ".jpg"))
                        {
                            System.IO.File.Delete(hosting.WebRootPath +
                                                  "\\images\\profiles\\" + cekUser.ID + ".jpg");
                        }
                    }
                    else
                    {
                        if (files.Count == 1)
                        {
                            var fStream = new FileStream(hosting.WebRootPath +
                                                         "\\images\\profiles\\" + cekUser.ID + ".jpg", FileMode.Create);
                            await files[0].CopyToAsync(fStream);
                            fStream.Dispose(); fStream = null;
                        }
                    }
                    logger.Write(LOG_MODE.ACTION, "ID " + GetID() + " commit update to user(" + result.Entity.ID + ")");
                } catch (Exception exc) {
                    return(Send(new {
                            status = "Error", desc = exc.Message, number = exc.HResult,
                            source = exc.Source, sTrace = exc.StackTrace
                        }));
                }
                return(Send(new {
                        status = "success"
                    }));
            }

            case "delete": {
                try {
                    checker = new string[] {
                        "id"
                    };
                    if (!CheckPayLoad(checker))
                    {
                        return(ErrorInvalid());
                    }
                    if (!CheckAccess(ACCESS.USER_DELETE))
                    {
                        return(ErrorAccess());
                    }
                    if (!ModelState.IsValid)
                    {
                        return(ErrorBadReq());
                    }

                    var cekUser = await db.users.SingleOrDefaultAsync(user => user.ID == Convert.ToInt32(Request.Form["id"]));

                    if (cekUser == null)
                    {
                        return(ErrorUserNotFound());
                    }
                    if (GetID() == cekUser.ID)
                    {
                        return(Send(new {
                                status = "error", desc = "You can't delete yourself"
                            }));
                    }
                    var result = db.users.Remove(cekUser);
                    if (result.State == EntityState.Deleted)
                    {
                        result.Context.SaveChanges();
                    }
                    else if (result.State == EntityState.Unchanged)
                    {
                        return(Send(new {
                                status = "error", desc = "Can't delete existing User, due to state is remainly unchanged"
                            }));
                    }
                    logger.Write(LOG_MODE.ACTION, "ID " + GetID() + " delete user(" + result.Entity.ID + ")");
                } catch (Exception exc) {
                    return(Send(new {
                            status = "Error", desc = exc.Message, number = exc.HResult,
                            source = exc.Source, sTrace = exc.StackTrace
                        }));
                }
                return(Send(new {
                        status = "success"
                    }));
            }

            case "change_pw": {
                try {
                    checker = new string[] {
                        "new", "old"
                    };
                    if (!CheckPayLoad(checker))
                    {
                        return(ErrorInvalid());
                    }
                    if (!IsAuthenticated())
                    {
                        return(ErrorAccess());
                    }
                    if (!ModelState.IsValid)
                    {
                        return(ErrorBadReq());
                    }

                    var cekUser = await db.users.SingleOrDefaultAsync(user => user.ID == GetID());

                    if (cekUser == null)
                    {
                        return(ErrorUserNotFound());
                    }
                    Simple3Des des = new Simple3Des(cekUser.Username);
                    if (cekUser.Password != des.EncryptData(Request.Form["old"]))
                    {
                        return(Send(new { status = "error", desc = "Old password didn't match" }));
                    }
                    cekUser.Password = des.EncryptData(Request.Form["new"]);
                    var result = db.users.Update(cekUser);
                    if (result.State == EntityState.Modified)
                    {
                        await result.Context.SaveChangesAsync();
                    }
                    else if (result.State == EntityState.Unchanged)
                    {
                        return(Send(new {
                                status = "error", desc = "Can't change password, due to state is remainly unchanged"
                            }));
                    }
                } catch (Exception exc) {
                    return(Send(new {
                            status = "Error", desc = exc.Message, number = exc.HResult,
                            source = exc.Source, sTrace = exc.StackTrace
                        }));
                }
                return(Send(new {
                        status = "success"
                    }));
            }

            /*case "checker": {
             *  if (!CheckPayLoad(new string[] {
             *      "uid"
             *  })) return ErrorInvalid();
             *  return "";
             * }*/
            default:
                return(ErrorInvalid());
            }
        }