public JsonResult IndexPost(int groupId, UserData userData) { var user = GetUser(); var group = DataAccess.Query<Group>().FirstOrDefault(g => g.Id == groupId); if (group == null) { throw new Exception(string.Format("Group with id {0} not found", groupId)); } var date = DateTime.Now.AddHours(-int.Parse(ConfigurationManager.AppSettings["CHECKIN_LENGTH"])); var checkIn = DataAccess.Query<CheckIn>().FirstOrDefault(c => c.User.Id == user.Id && c.Time > date); if (checkIn == null) { checkIn = new CheckIn { User = user, Time = DateTime.Now, Group = group, Code = new Random(DateTime.Now.Millisecond).Next().ToString() }; DataAccess.Save<CheckIn>(checkIn); } return Json(checkIn); }
public ActionResult LoginRedirect(string code) { if (User.Identity.IsAuthenticated) { return RedirectToAction("index", "main"); } try { var template = "https://oauth.vk.com/access_token?client_id={0}&client_secret={1}&code={2}"; var accessTokenString = HttpHelper.Get(string.Format(template, ConfigurationManager.AppSettings["VK_API"], ConfigurationManager.AppSettings["VK_SECRET"], code)); var serializer = new JavaScriptSerializer(); var accessToken = serializer.Deserialize<AccessTokenResult>(accessTokenString); var vk_user = VKHelper.GetUsers(new[] { accessToken.User_id }, accessToken.Access_token).FirstOrDefault(); var user = DataAccess.Query<User>().FirstOrDefault(u => u.VKId == vk_user.uid); if (user == null) { user = new User { VKId = vk_user.uid, FirstName = vk_user.first_name, LastName = vk_user.last_name }; } else { user.FirstName = vk_user.first_name; user.LastName = vk_user.last_name; } DataAccess.Save<User>(user); var userData = new UserData { AccessToken = accessToken.Access_token }; var ticket = new FormsAuthenticationTicket(1, user.Id.ToString(), DateTime.Now, DateTime.Now.AddSeconds(accessToken.Expires_in), true, userData.Json()); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); Response.Cookies.Add(cookie); return RedirectToAction("index", "main"); } catch { return RedirectToAction("index", "auth"); } }
public JsonResult IndexPut(int groupId, UserData userData) { return IndexPost(groupId, userData); }