/// <summary> /// /// </summary> /// <param name="loginInfo"></param> /// <returns></returns> private DTOAPIRes_Login LoginUnionWay(DTOAPIReq_Login loginInfo) { var entity = this.accesser.db.Accounts; IQueryable <Account> query_union = this.accesser.db.Accounts.Where(x => 1 == 0); try { Int64 key = Int64.Parse(loginInfo.username); entity.Where(x => x.Id == key); query_union = query_union.Union(entity.Where(x => x.Id == key)); } catch (Exception ex) { } if (PhoneHelper.IsValid(loginInfo.username)) { var data = PhoneHelper.Split(loginInfo.username); var areacode = data.Item1; var phone = data.Item2; query_union = query_union.Union(entity.Where(x => x.PhoneAreaCode == areacode && x.Phone == phone)); } if (EmailHepler.IsValid(loginInfo.username)) { string email = loginInfo.username.ToLower(); query_union = query_union.Union(entity.Where(x => x.Email.Equals(email))); } query_union = query_union.Union(entity.Where(x => x.Username == loginInfo.username)); query_union = query_union.Union(entity.Where(x => x.Passport.Equals(loginInfo.username.ToLower()))); #if DEBUG #endif var arr = query_union.ToArray(); if (arr != null && arr.Length > 0) { var account = arr.Where(x => x.Password == loginInfo.password).SingleOrDefault(); return(GenLoginData(account, loginInfo.password)); } else { return(new DTOAPIRes_Login { accessToken = "", state = 3, msg = "UID/通行证/用户名/邮箱/手机号不存在" }); } //return account; }
public async Task <IActionResult> Login([FromBody] DTOAPIReq_Login userInfo) { try { var dataInfo = await this.services.Login(userInfo).ConfigureAwait(false); return(JsonToCamelCase(dataInfo)); } catch (Exception ex) { return(JsonToCamelCase(ex.Message, 50000, 50000)); } }
public IActionResult JWTLogin([FromBody] DTOAPIReq_Login userInfo) { if (userInfo == null) { return(NotFound()); } if (!string.IsNullOrEmpty(userInfo.username) && !string.IsNullOrEmpty(userInfo.password)) { Claim[] claims = new[] { // 时间戳 new Claim(JwtRegisteredClaimNames.Nbf, $"{ new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds() }"), // 过期日期 new Claim(JwtRegisteredClaimNames.Exp, $"{ new DateTimeOffset(DateTime.Now.AddMinutes(30)).ToUnixTimeSeconds() }"), // 用户标识 new Claim(ClaimTypes.Name, userInfo.username), // Custom Data new Claim("customType", "hi ! LinQing") }; // Key SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(GJWT.SecurityKey)); // 加密方式 SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); // Token JwtSecurityToken token = new JwtSecurityToken( issuer: GJWT.Domain, audience: GJWT.Domain, claims: claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); return(OkEx(new { accessToken = new JwtSecurityTokenHandler().WriteToken(token) })); } else { return(BadRequest(new { message = "username or password is incorrect." })); } }
/// <summary> /// 登录操作 /// </summary> /// <param name="controller"></param> /// <param name="data"></param> /// <returns></returns> static public EM_LoginState LoginLogic(this Controller controller, DTOAPIReq_Login data) { CoreContext db = new CoreContext(); Account account = (from x in db.Accounts.Include(obj => obj.AccountRoles) where x.Username == data.username select x).FirstOrDefault(); if (account == null) { return(EM_LoginState.NoExist); } if (account.Password == data.password) { IList <string> roles = (from x in account.AccountRoles select x.role.RoleName).ToList(); DTO_StoreAccount storeAccount = new DTO_StoreAccount { Id = account.Id, username = account.Username, password = account.Password, avatar = account.Avatar, email = account.Email, name = account.DisplayName, introduction = account.Introduction, phone = account.Phone, roles = roles }; controller.HttpContext.Session.SetStoreAccount(storeAccount); return(EM_LoginState.Pass); } else { return(EM_LoginState.PasswordError); } }
/// <summary> /// /// </summary> /// <param name="LoginInfo"></param> /// <returns></returns> public async Task <dynamic> Login(DTOAPIReq_Login LoginInfo) { return(LoginUnionWay(LoginInfo)); }