public JsonResult FindPwd(UserAccount param) { if (!ModelState.IsValid) { return(Json(new ReturnResult(2, Global.VerifyModel(ModelState)))); } var code = MeCache <string> .Get(string.Format(MeCacheKey.EmailVerifyCode, param.Account) + "找回密码"); if (code != param.VerifyCode_Email) { return(Json(new ReturnResult(2, "邮箱验证码错误"))); } using (var db = new DataMigrationEntities()) { var user_account = db.user_account.FirstOrDefault(a => a.Email == param.Account); if (user_account == null) { return(Json(ReturnResult.NotExist)); } user_account.Password = param.Password; user_account.Modifier = "self"; user_account.ModifyTime = DateTime.Now; db.SaveChanges(); return(Json(ReturnResult.Ok)); } }
/// <summary> /// 过滤未登录的或过期请求 /// </summary> /// <param name="actionContext"></param> protected override bool IsAuthorized(HttpActionContext actionContext) { // base.OnAuthorization(actionContext); var token = actionContext.Request.Headers.Authorization; if (token == null || string.IsNullOrWhiteSpace(token.Parameter)) { Logger.Instance.Info($"Token 是空的 请求的Uri:{JsonConvert.SerializeObject(actionContext.Request.RequestUri)}"); return(false); } var userinfo = MeCache <UserInfo> .Get(token.Parameter); if (userinfo == null) { Logger.Instance.Info($"Userinfo 是空的 请求的Uri:{JsonConvert.SerializeObject(actionContext.Request.RequestUri)}"); return(false); } if (userinfo.IsLock == true) { Logger.Instance.Print($"{token.Parameter} 账号锁定{userinfo.UserId}"); actionContext.Response = actionContext.Request.CreateResponse( HttpStatusCode.Forbidden, new { code = 403, message = "您的账号已被封号,具体咨询平台客服" }); return(true); } return(true); }
public JsonResult Login(UserAccount param) { var token = Guid.NewGuid().ToString(); UserInfo userInfo = null; if (!ModelState.IsValid) { return(Json(Global.VerifyModel(ModelState))); } using (var db = new DataMigrationEntities()) { var user_account = db.user_account.FirstOrDefault(a => a.Email == param.Account); if (user_account == null) { //注册流程 //验证账号 var vCode = MeCache <string> .Get(string.Format(MeCacheKey.EmailVerifyCode, param)); if (string.IsNullOrWhiteSpace(vCode)) { var json = SendVerifyCodeToEmail(param); return(SendVerifyCode(param)); } if (param.Code != vCode) { return(Json(ReturnResult.Fail(null))); } user_account = new user_account { CreateTime = DateTime.Now, Creator = "login_api", Modifier = string.Empty, ModifyTime = DateTime.Now, Email = param.Account, Account = param.Account, HeadPortrait = string.Empty, Mobile = string.Empty, Nickname = string.Empty, Password = "******", TrueName = string.Empty, }; db.user_account.Add(user_account); db.SaveChanges(); userInfo = SetCache(user_account, token); return(Json(new ReturnResult(0, token, userInfo))); } if (user_account.Password != param.Code) { return(Json(new ReturnResult(2, TipString.密码错误))); } userInfo = SetCache(user_account, token); return(Json(new ReturnResult(0, token, userInfo))); } }
/// <summary> /// /// </summary> /// <returns></returns> public static UserInfo GetUserInfo() { var value = HttpContext.Current.Request.Headers["Authorization"]; if (value == null) { throw new Exception("授权失效,请您重新登录。"); } var tokens = value.Split(' '); if (tokens == null || tokens.Length < 2) { throw new Exception("授权失效,请您重新登录。"); } return(MeCache <UserInfo> .Get(tokens[1])); }
/// <summary> /// 发送邮箱验证码 /// </summary> /// <param name="user_account"></param> /// <param name="token"></param> /// <returns></returns> private static ReturnResult SendVerifyCodeToEmail(UserAccount account, string functionName = "") { try { var key = string.Format(MeCacheKey.EmailVerifyCode, account.Account) + functionName; var data = MeCache <string> .Get(key); if (string.IsNullOrWhiteSpace(data)) { var vCode = new Random().Next(100000, 999999).ToString(); var param = new Dictionary <string, string> { { "Recipient", account.Account }, { "Content", "<p>您" + functionName + "的邮箱验证码为:<h1 style=\"color:red;\">" + vCode + "</h1></p>" }, { "CName", "【工务园系统】" } }; using (var http = new HttpClient()) { var response = http.PostAsJsonAsync("http://111.231.116.56:8080/api/email/send", param).Result; if (!response.IsSuccessStatusCode) { var content = "无内容"; try { content = response.Content.ReadAsStringAsync().Result; } catch (Exception) { } return(new ReturnResult(0, $"邮件发送服务异常. (错误代码:{response.StatusCode},响应内容:{content})")); } } //Vsan.Common.EmailHelper.SendMailb("*****@*****.**", "邮箱验证码", "gzxixoaaerawbgfj", account.Account, "你的邮箱验证码为:【" + vCode + "】 5分钟内有效.", vCode, "smtp.qq.com", false); MeCache <string> .AddOrUpdate(key, vCode, DateTime.Now.AddMinutes(5)); } return(new ReturnResult(0, "验证码已发送至你的邮箱,请查收")); } catch (Exception ex) { return(new ReturnResult(2, ex.Message + ex.InnerException?.Message + ex.InnerException?.InnerException?.Message)); } }
public JsonResult Register(UserAccount param) { if (!ModelState.IsValid) { return(Json(new ReturnResult(2, Global.VerifyModel(ModelState)))); } var code = MeCache <string> .Get(string.Format(MeCacheKey.EmailVerifyCode, param.Account)); if (code != param.VerifyCode_Email) { return(Json(new ReturnResult(2, "邮箱验证码错误"))); } using (var db = new DataMigrationEntities()) { var user_account = db.user_account.FirstOrDefault(a => a.Email == param.Account); if (user_account != null) { return(Json(ReturnResult.IsExist())); } user_account = new user_account { CreateTime = DateTime.Now, Creator = "login_api", Modifier = string.Empty, ModifyTime = DateTime.Now, Email = param.Account, Account = param.Account, HeadPortrait = string.Empty, Mobile = string.Empty, Nickname = string.Empty, Password = param.Password, TrueName = string.Empty, }; db.user_account.Add(user_account); db.SaveChanges(); return(Json(ReturnResult.Ok)); } }