Exemplo n.º 1
0
        public void AddUserToDb(CustomUser model)
        {
            using (var db = new PeopleDBEntities())
            {
                var entity = new User()
                {
                    Id = Guid.NewGuid(),
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Age = model.Age,
                    City = model.City,
                    AboutUser = model.Description,
                    Email = model.Email,
                    Password = Crypto.SHA256(model.Password),
                    Gender = CheckGender(model.Sex),
                    ImageBase64 = model.ImageBase64,
                    ImageContentType = model.ImageContentType,
                    RoleId = GetRole("user")
                };
                try
                {

                    db.User.Add(entity);
                    db.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }
        }
        protected override void Initialize(RequestContext requestContext)
        {
            ControllerContext = new ControllerContext(requestContext, this);

            using (var db = new PeopleDBEntities())
            {
                try
                {
                    HttpCookie authCookie = requestContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

                    string email = ticket.Name;
                    _user = db.User.FirstOrDefault(e => e.Email == email);
                }
                catch (NullReferenceException)
                {
                    new HttpException(403, "Forbidden");
                }

            }
        }