public ActionResult RegisterUser(string userName, string userEmail, string password) { var checkUser = userDataDapper.FindUser(userName); if (checkUser != null) { return(Json(new { success = false, error = "用户已存在!" })); } try { string token = Tools.GetMD5(encryptionTools.Crypt(userName + userEmail)); EmailInfo email = new EmailInfo(); email.Body = $"Hi,{userName}. <br>欢迎您注册地图搜租房(woyaozufang.live),你的账号已经注册成功." + "<br/>为了保证您能正常体验网站服务,请点击下面的链接完成邮箱验证以激活账号." + $"<br><a href='https://woyaozufang.live/Account/Activated?activatedCode={token}'>https://woyaozufang.live/Account/Activate?activatedCode={token}</a> " + "<br>如果您以上链接无法点击,您可以将以上链接复制并粘贴到浏览器地址栏打开." + "<br>此信由系统自动发出,系统不接收回信,因此请勿直接回复。" + "<br>如果有其他问题咨询请发邮件到[email protected]."; email.Receiver = userEmail; email.Subject = "地图找租房-激活账号"; email.ReceiverName = userName; emailService.Send(email); var user = new UserInfo(); user.UserName = userName; user.Password = password; user.Email = userEmail; user.ActivatedCode = token; userDataDapper.InsertUser(user); return(Json(new { success = true, message = "注册成功!" })); } catch (Exception ex) { return(Json(new { success = false, error = ex.ToString() })); } }
public ActionResult Callback(string code, string state) { if (!string.IsNullOrEmpty(code)) { try { var accessToken = _authClient.GetAccessToken(code).Result; var qqUser = _authClient.GetUserInfo(accessToken).Result; //未登录,通过此ID获取用户 var userInfo = _userDataDapper.FindUserByQQOpenUID(qqUser.Id); if (userInfo == null) { //新增用户 _userDataDapper.InsertUserForQQAuth(new UserInfo() { UserName = qqUser.Name, QQOpenUID = qqUser.Id }); userInfo = _userDataDapper.FindUserByQQOpenUID(qqUser.Id); } string token = _encryptionTools.Crypt($"{userInfo.ID}|{userInfo.UserName}"); _userService.WriteUserToken(userInfo, token); return(Ok(new { success = true, token = token, message = "登录成功!", data = userInfo })); } catch (Exception ex) { return(Ok(new { success = false, error = ex.ToString() })); } } return(Ok(new { success = false, error = "无效的auth code" })); }
public ActionResult SendRetrievePasswordEmail(string emailAccount) { try { var user = UserDataDapper.FindUser(emailAccount); var token = Tools.GetMD5(EncryptionTools.Crypt(user.UserName + user.Email + DateTime.Now.ToString())); EmailInfo email = new EmailInfo(); email.Body = $"Hi,{user.UserName}. <br>您正在通过注册邮箱找回密码,如果非本人操作,请勿继续." + "<br>请在24小时内点击以下链接重置密码:" + $"<br><a href='https://woyaozufang.live/Account/ModifyPassword?token={token}'>https://woyaozufang.live/Account/ModifyPassword?token={token}</a> " + "<br>如果您以上链接无法点击,您可以将以上链接复制并粘贴到浏览器地址栏打开." + "<br>此信由系统自动发出,系统不接收回信,因此请勿直接回复。" + "<br>如果有其他问题咨询请发邮件到[email protected]."; email.Receiver = user.Email; email.Subject = "地图找租房-找回密码"; email.ReceiverName = user.UserName; email.Send(); UserDataDapper.SaveRetrievePasswordToken(user.ID, token); return(Json(new { success = true })); } catch (Exception ex) { return(Json(new { success = false, error = ex.ToString() })); } }
public ActionResult Register([FromBody]UserSave registerUser) { if (registerUser == null || string.IsNullOrEmpty(registerUser.Email) || string.IsNullOrEmpty(registerUser.UserName)) { return Ok(new { success = false, error = "用户名/用户邮箱不能为空." }); } var checkUser = userDataDapper.FindUser(registerUser.UserName); if (checkUser != null) { return Ok(new { success = false, error = "用户已存在!" }); } try { string token = Tools.GetMD5(encryptionTools.Crypt(registerUser.UserName + registerUser.Email)); EmailInfo email = new EmailInfo(); email.Body = $"Hi,{registerUser.UserName}. <br>欢迎您注册地图搜租房(woyaozufang.live),你的账号已经注册成功." + "<br/>为了保证您能正常体验网站服务,请点击下面的链接完成邮箱验证以激活账号." + $"<br><a href='https://woyaozufang.live/Account/Activated?activatedCode={token}'>https://woyaozufang.live/Account/Activate?activatedCode={token}</a> " + "<br>如果您以上链接无法点击,您可以将以上链接复制并粘贴到浏览器地址栏打开." + "<br>此信由系统自动发出,系统不接收回信,因此请勿直接回复。" + "<br>如果有其他问题咨询请发邮件到[email protected]."; email.Receiver = registerUser.Email; email.Subject = "地图找租房-激活账号"; email.ReceiverName = registerUser.UserName; emailService.Send(email); var insertUser = new UserInfo(); insertUser.Email = registerUser.Email; insertUser.UserName = registerUser.UserName; insertUser.Password = insertUser.Password; insertUser.ActivatedCode = token; userDataDapper.InsertUser(insertUser); var userInfo = userDataDapper.FindUser(insertUser.UserName); string loginToken = encryptionTools.Crypt($"{userInfo.ID}|{userInfo.UserName}"); userService.WriteUserToken(userInfo, loginToken); return Ok(new { success = true, message = "注册成功!", token = loginToken, data = userInfo }); } catch (Exception ex) { return Ok(new { success = false, error = ex.ToString() }); } }
public ActionResult Login(string userName, string password) { //var loginUser = new UserInfo(){ Email="*****@*****.**", Password = "******", Status =1}; var loginUser = UserDataDapper.FindUser(userName); if (loginUser != null) { if (loginUser.Status != 1) { return(Json(new { success = false, error = "账号未激活/已被禁用,请点击激活邮件中的URL完成账号激活!" })); } if (loginUser.Password == Tools.GetMD5(password)) { var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userName), new Claim(ClaimTypes.Email, loginUser.Email), new Claim(ClaimTypes.NameIdentifier, loginUser.ID.ToString()) }, CookieAuthenticationDefaults.AuthenticationScheme)); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间 }).Wait(); string token = EncryptionTools.Crypt($"{loginUser.ID}|{loginUser.UserName}"); return(Json(new { success = true, token = token, messgae = "登录成功!" })); } else { return(Json(new { success = false, error = "密码错误!" })); } } else { return(Json(new { success = false, error = "找不到用户信息或密码错误!" })); } }