Esempio n. 1
0
        public ActionResult Delete(string id)
        {
            UserModel userModel = db.Users.Where(w => w.Username == id).SingleOrDefault();

            db.Users.Remove(userModel);
            db.SaveChanges();
            return(RedirectToAction("UserManagement"));
        }
Esempio n. 2
0
        public async Task <ActionResult> CreateWeather(string date, string cityName)
        {
            var url            = $"http://api.worldweatheronline.com/premium/v1/weather.ashx?key={apiKey}&q={cityName}&format=json&date={date}&lang=tr";
            var responseString = await client.GetStringAsync(url);

            var responseJson = JObject.Parse(responseString);
            var weather      = responseJson.SelectToken("data").SelectToken("weather")[0];

            using (var ctx = new IFSAppContext())
            {
                var wm = new WeatherModel()
                {
                    WeatherDate = DateTime.Parse(weather.SelectToken("date").ToString()),
                    CityName    = responseJson.SelectToken("data").SelectToken("request")[0].SelectToken("query").ToString().Split(',')[0],
                    Temperature = (int)weather.SelectToken("avgtempC"),
                    MainStatus  = weather.SelectToken("hourly")[4].SelectToken("lang_tr")[0].SelectToken("value").ToString(),
                    IconPath    = weather.SelectToken("hourly")[4].SelectToken("weatherIconUrl")[0].SelectToken("value").ToString()
                };
                bool isExists = ctx.WeatherInfos.Where(x => x.WeatherDate == wm.WeatherDate && x.CityName == wm.CityName).FirstOrDefault() != null;
                if (!isExists)
                {
                    ctx.WeatherInfos.Add(wm);
                    ctx.SaveChanges();
                }
                return(RedirectToAction("WeatherManagement"));
            }
        }
Esempio n. 3
0
 public ActionResult Register(RegisterModel model)
 {
     if (model == null)
     {
         ViewBag.LoginError = "Hatalı kullanıcı adı ya da şifre!";
         return(RedirectToAction("Login"));
     }
     else
     {
         using (var ctx = new IFSAppContext())
         {
             var u = ctx.Users.Where(w => w.Username == model.Username).SingleOrDefault();
             if (u != null)
             {
                 ViewBag.RegisterError = "Kullanıcı adı mevcut";
                 return(RedirectToAction("Login"));
             }
             UserModel user = new UserModel()
             {
                 DefaultCityName = model.DefaultCityName,
                 Name            = model.Name,
                 Username        = model.Username,
                 Password        = BCryptHelper.HashPassword(model.Password, BCryptHelper.GenerateSalt(12)),
                 UserType        = "Son Kullanıcı",
                 Status          = 1,
             };
             ctx.Users.Add(user);
             ctx.SaveChanges();
             return(RedirectToAction("Login"));
         }
     }
 }
Esempio n. 4
0
        public ActionResult Edit(RegisterModel model)
        {
            using (var ctx = new IFSAppContext())
            {
                var user = ctx.Users.Where(w => w.Username == model.Username).SingleOrDefault();//kullanıcyı bulabilmek için ön yüzden gizli bi parametre gönderdik
                user.DefaultCityName = !String.IsNullOrEmpty(model.DefaultCityName) ? model.DefaultCityName : user.DefaultCityName;
                user.Name            = model.Name ?? user.Name;
                user.Password        = model.Password != null?BCryptHelper.HashPassword(model.Password, BCryptHelper.GenerateSalt(12)) : user.Password;

                ctx.SaveChanges();
            }
            return(RedirectToAction("UserManagement"));
        }
Esempio n. 5
0
        public ActionResult EditProfile(RegisterModel model)
        {
            using (var ctx = new IFSAppContext())
            {
                var user = ctx.Users.Where(w => w.Username == User.Identity.Name).SingleOrDefault();
                user.DefaultCityName = model.DefaultCityName ?? user.DefaultCityName;
                user.Name            = model.Name ?? user.Name;
                user.Username        = model.Username ?? user.Username;
                user.Password        = model.Password != null?BCryptHelper.HashPassword(model.Password, BCryptHelper.GenerateSalt(12)) : user.Password;

                ctx.SaveChanges();
            }
            return(RedirectToAction("Profile"));
        }
Esempio n. 6
0
        public async Task <ActionResult> UpdateWeatherInfo()
        {
            List <string> cities = new List <string>();

            cities.Add("istanbul");
            cities.Add("izmir");
            cities.Add("ankara");
            cities.Add("sakarya");
            using (var ctx = new IFSAppContext())
            {
                var user = ctx.Users.Where(w => w.Username == User.Identity.Name).SingleOrDefault();
                foreach (var city in cities)
                {
                    var url            = $"http://api.worldweatheronline.com/premium/v1/weather.ashx?key={apiKey}&q={city}&format=json&num_of_days=7&lang=tr";
                    var responseString = await client.GetStringAsync(url);

                    var responseJson          = JObject.Parse(responseString);
                    List <WeatherModel> wlist = new List <WeatherModel>();
                    var weatherList           = responseJson.SelectToken("data").SelectToken("weather");
                    foreach (var w in weatherList)
                    {
                        var wm = new WeatherModel()
                        {
                            WeatherDate = DateTime.Parse(w.SelectToken("date").ToString()),
                            CityName    = responseJson.SelectToken("data").SelectToken("request")[0].SelectToken("query").ToString().Split(',')[0],
                            Temperature = (int)w.SelectToken("avgtempC"),
                            MainStatus  = w.SelectToken("hourly")[4].SelectToken("lang_tr")[0].SelectToken("value").ToString(),
                            IconPath    = w.SelectToken("hourly")[4].SelectToken("weatherIconUrl")[0].SelectToken("value").ToString()
                        };
                        bool isExists = ctx.WeatherInfos.Where(x => x.WeatherDate == wm.WeatherDate && x.CityName == wm.CityName).FirstOrDefault() != null;
                        if (!isExists)
                        {
                            wlist.Add(wm);
                        }
                    }
                    ctx.WeatherInfos.AddRange(wlist);
                }
                ctx.UserLogs.Add(new UserLogModel()
                {
                    LogTime   = DateTime.Now,
                    Username  = user.Username,
                    IPAddress = Request.UserHostAddress,
                    Log       = "Yönetici veri çekti"
                });
                ctx.SaveChanges();
            }
            return(RedirectToAction("Admin"));
        }
Esempio n. 7
0
 public ActionResult Login(LoginModel model)
 {
     if (model == null)
     {
         ViewBag.LoginError = "Hatalı giriş!";
         return(View());
     }
     if (attemptCount > 2)                                                  //3 kez hatalı giriş yaptıktan sonra çalışır
     {
         if (!(DateTime.Now - lastLoginAttempt >= TimeSpan.FromMinutes(1))) //bir dakika geçti mi? sayfa yenilendikten sonra
         {
             ViewBag.LoginError = $"3 kez üst üste hatalı giriş. {(60 - (DateTime.Now - lastLoginAttempt).TotalSeconds).ToString("##")} saniye kaldı";
             return(View());
         }
         attemptCount = 0;
     }
     using (var ctx = new IFSAppContext())
     {
         var u = ctx.Users.Where(w => w.Username == model.Username).SingleOrDefault();
         if (u != null && BCryptHelper.CheckPassword(model.Password, u.Password))
         {
             FormsAuthentication.SetAuthCookie(model.Username, false);
             ctx.UserLogs.Add(new UserLogModel
             {
                 LogTime   = DateTime.Now,
                 Username  = u.Username,
                 IPAddress = Request.UserHostAddress,
                 Log       = "Kullanıcı giriş yaptı",
             });
             ctx.SaveChanges();
             attemptCount = 0;
             return(RedirectToAction("Index"));
         }
         attemptCount++;
         lastLoginAttempt = DateTime.Now;
         if (attemptCount > 2)
         {
             ViewBag.LoginError = $"3 kez üst üste hatalı giriş. 60 saniye kaldı";
             return(View());
         }
         ViewBag.LoginError = "Kullanıcı adı ya da şifre yanlış!";
         return(View());
     }
 }