Exemplo n.º 1
0
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            string token = GetToken(actionContext);

            try {
                if (token == null)
                {
                    throw new Exception();
                }
                string   decoded   = Encoding.UTF8.GetString(Convert.FromBase64String(token));
                string[] creds     = decoded.Split(':');
                string   username  = creds[0];
                string   userToken = creds[1];
                using (APIEntities api = new APIEntities())
                {
                    User user = api.Users.Where(u => u.username.Equals(username)).FirstOrDefault();
                    if (user == null)
                    {
                        throw new Exception();
                    }
                    if (!user.token.Equals(userToken))
                    {
                        throw new Exception();
                    }

                    Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user.username), null);
                }
            }
            catch (Exception e)
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
            }
            base.OnAuthorization(actionContext);
        }
Exemplo n.º 2
0
 public ActionResult PostLogin(User user)
 {
     if (ModelState.IsValid)
     {
         using (APIEntities api = new APIEntities())
         {
             User usr = api.Users.FirstOrDefault(u =>
                                                 u.username.Equals(user.username) &&
                                                 u.password.Equals(user.password)
                                                 );
             if (usr != null)
             {
                 string microSeconds = DateTime.Now.ToString("HH:mm:ss.ffffff");
                 string token        = Convert.ToBase64String(Encoding.UTF8.GetBytes(microSeconds));
                 usr.token = token;
                 api.SaveChanges();
                 Session.Add("user", new UserDTO {
                     UserId = usr.user_id, Username = usr.username, Token = token
                 });
                 return(Redirect("~/dashboard"));
             }
         }
     }
     return(View("Login"));
 }
Exemplo n.º 3
0
 public string GetTodoDelete(int id)
 {
     using (APIEntities api = new APIEntities())
     {
         var todo = api.todoes.Find(id);
         api.todoes.Remove(todo);
         api.SaveChanges();
         return("success delete ID :" + id);
     }
 }
Exemplo n.º 4
0
 public string GetTodoCompleted(int id)
 {
     using (APIEntities api = new APIEntities())
     {
         var todo      = api.todoes.Find(id);
         var completed = todo.completed;
         todo.completed = (completed == 1) ? 0 : 1;
         api.SaveChanges();
         return("success completed ID :" + id);
     }
 }
Exemplo n.º 5
0
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
        {
            try
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                var jsonString = await request.Content.ReadAsStringAsync();
                RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
                //備份進來的json string
                logger.Info((jsonString));

                using (var db = new APIEntities())
                {

                    //抓取第一層tag
                    foreach (var tag in obj.tags)
                    {
                        //抓取tag下的item list物件
                        List<Item> oItem = new List<Item>(tag.items);

                        foreach (var tItem in oItem)
                        {
                            db.ExtData.Add(new ExtData()

                            {
                                store_id = obj.store_id,
                                data_generated_dtime = obj.data_generated_dtime,
                                data_start_dtime = obj.data_start_dtime,
                                data_end_dtime = obj.data_end_dtime,
                                tag_id = tag.id,
                                tag_external_id = tag.external_id,
                                tag_setting_external_id = tag.setting_external_id,
                                tag_name = tag.name,
                                item_id = tItem.id,
                                item_external_id = tItem.external_id,
                                item_setting_external_id = tItem.setting_external_id,
                                item_name = tItem.name

                            });
                        }
                    }
                    db.SaveChanges();
                }
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Fatal(LogUtility.BuildExceptionMessage(ex));
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }

        }
Exemplo n.º 6
0
        public ActionResult Logout()
        {
            UserDTO user = (UserDTO)Session["user"];

            using (APIEntities api = new APIEntities())
            {
                User u = api.Users.Find(user.UserId);
                u.token = null;
                api.SaveChanges();
            }
            Session["user"] = null;
            return(Redirect("~/login"));
        }
Exemplo n.º 7
0
 // GET api/values
 public IEnumerable <ItemDTO> Get()
 {
     using (APIEntities api = new APIEntities())
     {
         return(api.Items.Select(i =>
                                 new ItemDTO {
             ItemId = i.item_id,
             Name = i.name,
             Price = i.price,
             Stock = i.stock
         }
                                 ).ToList());
     }
 }
Exemplo n.º 8
0
 public string PostTodo(ToDoDTO todo)
 {
     using (APIEntities api = new APIEntities())
     {
         var newTodo = new todo
         {
             todo_id   = todo.ID,
             value     = todo.Value,
             completed = (todo.Completed) ? 1 : 0
         };
         api.todoes.Add(newTodo);
         api.SaveChanges();
         return("success");
     }
 }
Exemplo n.º 9
0
 public List <ToDoDTO> GetTodos()
 {
     using (APIEntities api = new APIEntities())
     {
         var todos    = api.todoes.ToList();
         var todoList = (from todo todo in todos
                         select new ToDoDTO
         {
             ID = todo.todo_id,
             Value = todo.value,
             Completed = (todo.completed == 1 ? true : false)
         }
                         );
         return(todoList.ToList());
     }
 }
Exemplo n.º 10
0
        // POST api/<controller>
        public async Task <HttpResponseMessage> Post(HttpRequestMessage request)
        {
            try
            {
                Logger logger     = LogManager.GetCurrentClassLogger();
                var    jsonString = await request.Content.ReadAsStringAsync();

                RootObject obj = JsonConvert.DeserializeObject <RootObject>(jsonString);
                //備份進來的json string
                logger.Info((jsonString));

                using (var db = new APIEntities())
                {   //直接第一層讀取
                    foreach (var item in obj.items)
                    {
                        db.RealData.Add(new RealData()
                        {
                            store_id             = obj.store_id,
                            data_generated_dtime = obj.data_generated_dtime,
                            data_start_dtime     = obj.data_start_dtime,
                            data_end_dtime       = obj.data_end_dtime,
                            item_id                  = item.id,
                            item_external_id         = item.external_id,
                            item_setting_external_id = item.setting_external_id,
                            item_name                = item.name,
                            item_category_id         = item.category_id,
                            item_category_name       = item.category_name,
                            quantity                 = Convert.ToDecimal(item.quantity)
                        });

                        db.SaveChanges();
                    }
                    return(new HttpResponseMessage(HttpStatusCode.OK));
                }
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Fatal(LogUtility.BuildExceptionMessage(ex));
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
Exemplo n.º 11
0
        // GET: HotSpot
        public async Task <ActionResult> Index()
        {
            string targetURI = "http://data.ntpc.gov.tw/od/data/api/04958686-1B92-4B74-889D-9F34409B272B?$format=json";

            HttpClient client = new HttpClient();

            client.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await client.GetStringAsync(targetURI);

            var collection = JsonConvert.DeserializeObject <IEnumerable <HotSpot> >(response);

            {
                using (var db = new APIEntities())
                {
                    List <HotSpotDB> hdb = new List <HotSpotDB>();

                    foreach (var item in collection)
                    {
                        db.HotSpotDB.Add(new HotSpotDB()
                        {
                            id             = item.id,
                            spot_name      = item.spot_name,
                            type           = item.type,
                            company        = item.company,
                            district       = item.district,
                            address        = item.address,
                            apparatus_name = item.apparatus_name,
                            latitude       = item.latitude,
                            longitude      = item.longitude
                        });
                    }
                    //hdb.ToList();

                    db.SaveChanges();
                }
            }

            //ViewBag.Result = response;
            return(View());
        }