public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new User() { UserName = model.UserName, Email = model.Email, BirthDate = model.BirthDate }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } } else { EditErrorMessageConfirmPassword(); EditErrorMessageBirthDate(); } // Process model errors. return View(model); }
public static async Task PauseGame(User user, string gameId, DateTime dateSuspended, CancellationToken token) { using (var context = new MagicDbContext()) { var gameHubContext = GlobalHost.ConnectionManager.GetHubContext<GameHub>(); gameHubContext.Clients.Group(gameId).pauseGame("has paused the game.", user.UserName, user.ColorCode); var pause = Task.Delay(10000, token); var chatRoom = context.ChatRooms.Find(gameId); var chatHubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); chatHubContext.Clients.Group(gameId).addMessage(gameId, DateTime.Now.ToString("HH:mm:ss"), user.UserName, user.ColorCode, "has paused the game."); var game = context.Games.Find(gameId); game.UpdateTimePlayed(dateSuspended); try { await pause; } catch (OperationCanceledException) { } finally { game.DateResumed = DateTime.Now; gameHubContext.Clients.Group(gameId).activateGame(game.TimePlayed.ToTotalHoursString()); context.InsertOrUpdate(game, true); } } }
private async Task SignInAsync(User user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); user.LastLoginDate = DateTime.Now; context.InsertOrUpdate(user); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new User() { UserName = model.UserName, Email = model.Email, BirthDate = model.BirthDate }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { result = await UserManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } } else { EditErrorMessageConfirmPassword(); EditErrorMessageBirthDate(); } ViewBag.ReturnUrl = returnUrl; return View(model); }
private string DecodeRecipient(string messageText, out User recipient) { var recipientName = System.Text.RegularExpressions.Regex.Match(messageText, "^@([a-zA-Z]+[a-zA-Z0-9]*(-|\\.|_)?[a-zA-Z0-9]+)").Value; if (recipientName.Length > 0) { recipientName = recipientName.Substring(1); } // Try to find recipient. using (var context = new MagicDbContext()) { recipient = context.Users.FirstOrDefault(u => u.UserName == recipientName); } return recipientName; }
private bool ValidateMessage(string messageText, string recipientName, out User recipient) { recipient = null; if (messageText.Length <= 0) return false; // No message text at all. if (recipientName.Length <= 0) return true; // Group chat message. using (var context = new MagicDbContext()) { // Look for recipient. recipient = context.Users.FirstOrDefault(u => u.UserName == recipientName); if (recipient == null) { // Recipient included but invalid, alert sender. Clients.Caller.addMessage(DefaultRoomId, DateTime.Now.ToString("HH:mm:ss"), "ServerInfo", "#000000", "- no such user found, have you misspelled the name?", recipientName, "#696969"); return false; } // Valid recipient found. if (recipient.Status == UserStatus.Offline) { // Valid recipient but is offline, alert sender. Clients.Caller.addMessage(DefaultRoomId, DateTime.Now.ToString("HH:mm:ss"), "ServerInfo", "#000000", "is currently offline and unable to receive messages.", recipient.UserName, recipient.ColorCode); return false; } return true; // Valid message for recipient. } }