private async Task SignInAsync(WhoaVerseUser user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { return(View(model)); } // begin recaptcha check string encodedResponse = Request.Form["g-Recaptcha-Response"]; bool isCaptchaCodeValid = (ReCaptchaUtility.Validate(encodedResponse) == "True" ? true : false); if (!isCaptchaCodeValid) { ModelState.AddModelError("", "Incorrect recaptcha answer."); return(View()); } // end recaptcha check try { // get user IP address string clientIpAddress = Utils.User.UserIpAddress(Request); var user = new WhoaVerseUser { UserName = model.UserName, RegistrationDateTime = DateTime.Now, LastLoginFromIp = clientIpAddress, LastLoginDateTime = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); // redirect new users to Welcome actionresult return(RedirectToAction("Welcome", "Home")); } AddErrors(result); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse."); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { return(View(model)); } try { // get user IP address string clientIpAddress = Utils.User.UserIpAddress(Request); // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress); if (accountsWithSameIp >= MvcApplication.MaxAllowedAccountsFromSingleIP) { ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account."); return(View(model)); } var user = new WhoaVerseUser { UserName = model.UserName, RegistrationDateTime = DateTime.Now, LastLoginFromIp = clientIpAddress, LastLoginDateTime = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); // redirect new users to Welcome actionresult return(RedirectToAction("Welcome", "Home")); } AddErrors(result); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse."); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { return(View(model)); } // begin recaptcha check const string captchaMessage = ""; var isCaptchaCodeValid = ReCaptchaUtility.GetCaptchaResponse(captchaMessage, Request); if (!isCaptchaCodeValid) { ModelState.AddModelError("", "Incorrect recaptcha answer."); return(View()); } // end recaptcha check try { var user = new WhoaVerseUser { UserName = model.UserName, RegistrationDateTime = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent : false); // redirect new users to Welcome actionresult return(RedirectToAction("Welcome", "Home")); } AddErrors(result); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse."); } // If we got this far, something failed, redisplay form return(View(model)); }
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 WhoaVerseUser { UserName = model.UserName }; 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)); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return(View(model)); }
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 WhoaVerseUser { UserName = model.UserName }; 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); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); try { // get user IP address string clientIpAddress = Utils.User.UserIpAddress(Request); // check the number of accounts already in database with this IP address, if number is higher than max conf, refuse registration request var accountsWithSameIp = UserManager.Users.Count(x => x.LastLoginFromIp == clientIpAddress); if (accountsWithSameIp >= MvcApplication.MaxAllowedAccountsFromSingleIP) { ModelState.AddModelError(string.Empty, "This device can not be used to create a voat account."); return View(model); } var user = new WhoaVerseUser { UserName = model.UserName, RegistrationDateTime = DateTime.Now, LastLoginFromIp = clientIpAddress, LastLoginDateTime = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); // redirect new users to Welcome actionresult return RedirectToAction("Welcome", "Home"); } AddErrors(result); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse."); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); try { // get user IP address string clientIpAddress = Utils.User.UserIpAddress(Request); var user = new WhoaVerseUser { UserName = model.UserName, RegistrationDateTime = DateTime.Now, LastLoginFromIp = clientIpAddress, LastLoginDateTime = DateTime.Now }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); // redirect new users to Welcome actionresult return RedirectToAction("Welcome", "Home"); } AddErrors(result); } catch (Exception) { ModelState.AddModelError(string.Empty, "Something bad happened. You broke Whoaverse."); } // If we got this far, something failed, redisplay form return View(model); }