Exemplo n.º 1
0
        public void AddUser(User user)
        {
            UserDa da = new UserDa(UnitOfData);

            if (user == null)
            {
                user = new User()
                {
                    FID = "1", NickName = "ss"
                };
            }
            da.Add(user);
        }
        public ActionResult processRegister()
        {
            VerifyDbContext context = new VerifyDbContext();
            //check đã chụp ảnh chưa
            string path1 = Server.MapPath(IMAGE_DIR + "RegisterImage1.jpg");
            string path2 = Server.MapPath(IMAGE_DIR + "RegisterImage2.jpg");
            string path3 = Server.MapPath(IMAGE_DIR + "RegisterImage3.jpg");

            string dir1 = Directory.GetParent(path1).FullName;
            string dir2 = Directory.GetParent(path2).FullName;
            string dir3 = Directory.GetParent(path3).FullName;

            if (!System.IO.File.Exists(path1) && !System.IO.File.Exists(path2) && !System.IO.File.Exists(path3))
            {
                TempData["error"] = "Bạn chưa chụp ảnh";
                return(RedirectToAction("Register", "Login"));
            }
            else
            {
                NameValueCollection form = Request.Form;
                string fullName = "", email = "", personId = "";
                string sex;
                fullName = form["fullName"];
                email    = form["email"];
                sex      = form["optradio"];
                Debug.WriteLine(fullName + email + sex);


                //Goi hàm kiểm tra email đã tồn tại checkEmail(email); trả về true nếu đã tồn tại trong database
                var userDa = new UserDa();
                var verificationDa = new VerificationDa();
                if (userDa.HasMailExisted(email))
                {
                    TempData["error"] = "Email " + email + " đã được sử dụng để đăng ký trước đây";
                    return(RedirectToAction("Register", "Login"));
                }
                else
                {
                    try
                    {
                        Debug.WriteLine(1);
                        //Tao mới person
                        var request =
                            (HttpWebRequest)
                            WebRequest.Create(
                                "https://api.projectoxford.ai/face/v1.0/persongroups/open_source_net1/persons");
                        var postData = "{\"name\":\"" + email + "\",\"userData\":\"user-provided data attached to the person group\"}";
                        //var imgBinary = File.ReadAllBytes(_imgPath1);

                        byte[] byteData = Encoding.UTF8.GetBytes(postData);
                        //var data = Encoding.ASCII.GetBytes(postData);

                        request.Method        = "POST";
                        request.ContentType   = "application/json";
                        request.ContentLength = byteData.Length;
                        request.Host          = "api.projectoxford.ai";
                        request.Headers.Add("Ocp-Apim-Subscription-Key", "1c056c36ece84f14a0619803ee4f0ceb");

                        using (var stream = request.GetRequestStream())
                        {
                            stream.Write(byteData, 0, byteData.Length);
                        }

                        var response = (HttpWebResponse)request.GetResponse();

                        var     responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        string  json           = responseString.ToString();
                        dynamic stuff          = JObject.Parse(json);
                        personId = stuff.personId;

                        //Vi - TODO: goi ham luu person moi vao database addPerson(fullName, email, personId);
                        var addedUser = userDa.Add(new User {
                            Email = email, Gender = sex, Name = fullName, PersonId = personId
                        });
                        int userID = addedUser.Id;

                        //add ảnh vào person
                        if (Directory.Exists(dir1))
                        {
                            Debug.WriteLine(2);
                            var request1 =
                                (HttpWebRequest)
                                WebRequest.Create(
                                    "https://api.projectoxford.ai/face/v1.0/persongroups/open_source_net1/persons/" + personId + "/persistedFaces");

                            Debug.WriteLine(path1);
                            var imgBinary = System.IO.File.ReadAllBytes(path1);


                            request1.Method        = "POST";
                            request1.ContentType   = "application/octet-stream";
                            request1.ContentLength = imgBinary.Length;
                            request1.Host          = "api.projectoxford.ai";
                            request1.Headers.Add("Ocp-Apim-Subscription-Key", "1c056c36ece84f14a0619803ee4f0ceb");

                            using (var stream = request1.GetRequestStream())
                            {
                                stream.Write(imgBinary, 0, imgBinary.Length);
                            }

                            var response1 = (HttpWebResponse)request1.GetResponse();

                            responseString = new StreamReader(response1.GetResponseStream()).ReadToEnd();
                            json           = responseString.ToString();
                            Debug.WriteLine(json);
                            stuff = JObject.Parse(json);

                            string persistedFaceId = stuff.persistedFaceId;
                            Debug.WriteLine(persistedFaceId);
                            //addIdentification(userId, persistedFaceId)
                            verificationDa.Add(new Verification {
                                PersistedFaceId = persistedFaceId, UserId = userID
                            });
                        }
                        if (System.IO.File.Exists(path2))
                        {
                            Debug.WriteLine(Directory.Exists(dir2));
                            var request1 =
                                (HttpWebRequest)
                                WebRequest.Create(
                                    "https://api.projectoxford.ai/face/v1.0/persongroups/open_source_net1/persons/" + personId + "/persistedFaces");


                            var imgBinary = System.IO.File.ReadAllBytes(path2);

                            request1.Method        = "POST";
                            request1.ContentType   = "application/octet-stream";
                            request1.ContentLength = imgBinary.Length;
                            request1.Host          = "api.projectoxford.ai";
                            request1.Headers.Add("Ocp-Apim-Subscription-Key", "1c056c36ece84f14a0619803ee4f0ceb");

                            using (var stream = request1.GetRequestStream())
                            {
                                stream.Write(imgBinary, 0, imgBinary.Length);
                            }

                            var response1 = (HttpWebResponse)request1.GetResponse();

                            responseString = new StreamReader(response1.GetResponseStream()).ReadToEnd();
                            json           = responseString.ToString();
                            stuff          = JObject.Parse(json);

                            string persistedFaceId = stuff.persistedFaceId;
                            Debug.WriteLine(persistedFaceId);
                            //addIdentification(userId, persistedFaceId)
                            verificationDa.Add(new Verification {
                                PersistedFaceId = persistedFaceId, UserId = userID
                            });
                        }
                        if (System.IO.File.Exists(path3))
                        {
                            Debug.WriteLine(4);
                            var request1 =
                                (HttpWebRequest)
                                WebRequest.Create(
                                    "https://api.projectoxford.ai/face/v1.0/persongroups/open_source_net1/persons/" + personId + "/persistedFaces");


                            var imgBinary = System.IO.File.ReadAllBytes(path3);

                            request1.Method        = "POST";
                            request1.ContentType   = "application/octet-stream";
                            request1.ContentLength = imgBinary.Length;
                            request1.Host          = "api.projectoxford.ai";
                            request1.Headers.Add("Ocp-Apim-Subscription-Key", "1c056c36ece84f14a0619803ee4f0ceb");

                            using (var stream = request1.GetRequestStream())
                            {
                                stream.Write(imgBinary, 0, imgBinary.Length);
                            }

                            var response1 = (HttpWebResponse)request1.GetResponse();

                            responseString = new StreamReader(response1.GetResponseStream()).ReadToEnd();
                            json           = responseString.ToString();
                            stuff          = JObject.Parse(json);

                            string persistedFaceId = stuff.persistedFaceId;
                            Debug.WriteLine(persistedFaceId);
                            //addIdentification(userId, persistedFaceId)
                            verificationDa.Add(new Verification {
                                PersistedFaceId = persistedFaceId, UserId = userID
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message);
                        Debug.WriteLine(ex.Message);
                        TempData["error"] = "Xảy ra lỗi trong quá trình giao tiếp với API, bạn vui long tiến hành đăng ký lại";
                        return(RedirectToAction("Register", "Login"));
                    }
                }
            }



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