public virtual ActionResult ResetPassword(string emailAddress) { // find out language var ctx = new DatabaseDataContext(); var id = ctx.GetUserIDByEmail(emailAddress.ToLower()); var client = new WebClient(); client.Encoding = Encoding.UTF8; var body = client.DownloadString(Request.Url.Host + "/en/mail/?view=EmailConfirmation&id=" + id.ToString()); var subject = client.ResponseHeaders["X-JaapMail-Subject"]; var recipient = client.ResponseHeaders["X-JaapMail-Recipient-Email"]; var name = client.ResponseHeaders["X-JaapMail-Recipient-Name"]; if (!string.IsNullOrWhiteSpace(name)) { name = name.Replace("<", ""); name = name.Replace(">", ""); } var error = client.ResponseHeaders["X-JaapMail-Error"]; SendMail(recipient, name, subject, body); return View(); }
public virtual ActionResult ExternalLoginCallback(string returnUrl) { var ctx = new DatabaseDataContext(); var bypass = ctx.GetSetting("ByPassPrefix"); AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); if (!result.IsSuccessful) { return RedirectToAction("ExternalLoginFailure"); } var email = result.ExtraData["username"].ToLower(); long id = -1; try { id = ctx.GetUserIDByEmail(email); } catch { } if (id == -1) { // new user should not get a confirmation mail try { id = ctx.RegisterNewUser( email, Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(new Guid().ToString()))), false, returnUrl ); } catch { } } string token = ""; if (id != -1) { try { token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext)); } catch (Exception exception) { switch (exception.ErrorCode()) { case 60021: ctx.ConfirmEmailAddress(Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(email)))); break; } } // second try if (string.IsNullOrWhiteSpace(token)) { try { token = ctx.LoginUser(bypass + "_" + email, "notneeded", MvcApplication.HostIPAddress(HttpContext)); } catch { } } } if (!string.IsNullOrWhiteSpace(token)) { var tokenCookie = new HttpCookie("GameToken", token); tokenCookie.Expires = DateTime.Now.AddYears(1); Response.Cookies.Add(tokenCookie); if (!string.IsNullOrWhiteSpace(returnUrl)) { if (returnUrl.Contains("{guid}")) { if (returnUrl.Contains("?")) returnUrl = returnUrl.Replace("{guid}", "&guid=" + token); else returnUrl = returnUrl.Replace("{guid}", "?guid=" + token); } } else { returnUrl = Url.Action(MVC.Home.Index()); } ViewBag.ReturnUrl = returnUrl; return View(MVC.Home.Views.Shared.ExternalLoginCallback); } return RedirectToAction("ExternalLoginFailure"); }
public virtual ActionResult ResetPassword(string emailAddress) { try { emailAddress = emailAddress.Trim(); try { emailAddress = NormalizeEmailAddress(emailAddress); } catch { throw new ApplicationException("60040 The supplied username is not a valid email address."); } long? userID = null; using (var ctx = new DatabaseDataContext()) { // find out language userID = ctx.GetUserIDByEmail(emailAddress.ToLower()); } if (!userID.HasValue) { throw new ApplicationException("60041 User not found."); } try { var password = Perpetuality.Utilities.ReadablePassword.GenerateReadablePassword(); // mail the new password var client = new WebClient(); client.Encoding = Encoding.UTF8; var body = client.DownloadString(ConfigurationManager.AppSettings["BaseURL"] + "/en/mail/?view=PasswordRequest&id=" + userID + "," + HttpUtility.UrlEncode(password)); var subject = client.ResponseHeaders["X-JaapMail-Subject"]; var recipient = client.ResponseHeaders["X-JaapMail-Recipient-Email"]; var name = client.ResponseHeaders["X-JaapMail-Recipient-Name"]; if (!string.IsNullOrWhiteSpace(name)) { name = name.Replace("<", ""); name = name.Replace(">", ""); } var error = client.ResponseHeaders["X-JaapMail-Error"]; SendMail(recipient, name, subject, body); // update the database with the new password using (var ctx = new DatabaseDataContext()) { ctx.ChangeUserPasswordInternal(userID, password, true); } } catch (Exception e) { throw new ApplicationException("60043 Password retrieval failed.", e); } } catch (Exception e) { EventLogger.WriteEvent(e.Message, EventLogger.EventType.Error, "Perpetuality"); } return View(); }