/// <summary> /// Home接口入口 /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <IActionResult> Index(int id) { switch (id) { //登录 case 200: return(await Func200()); //默认返回失败 default: return(Ok(APIResponse.GetResult(StateCode.State_6))); } }
/// <summary> /// 执行Action之前 /// </summary> /// <param name="context"></param> public async override void OnActionExecuting(ActionExecutingContext context) { //获取基类控制器 var baseController = ((BaseController)context.Controller); //获取请求参数 byte[] buffer = new byte[1024]; var len = await context.HttpContext.Request.Body.ReadAsync(buffer, 0, buffer.Length); List <byte> list = new List <byte>(); while (len > 0) { list.AddRange(buffer.Take(len)); //读取完成跳出循环 len = await context.HttpContext.Request.Body.ReadAsync(buffer, 0, buffer.Length); } //基类控制器 baseController.DynamicStr = Encoding.UTF8.GetString(list.ToArray()); //动态运行时对象 baseController.Dynamic = JsonConvert.DeserializeObject <dynamic>(baseController.DynamicStr); //post提交方式 if ("post".Equals(context.HttpContext.Request.Method.ToLower())) { if (context.HttpContext.User.Identity.IsAuthenticated) { string token = context.HttpContext.Request.Headers["Authorization"]; if (token.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) { token = token.Substring("Bearer ".Length).Trim(); } } //数据包 baseController.Sign = JsonConvert.DeserializeObject <SignPackage>(baseController.Dynamic.Global.ToString()); //签名校验 if (!WeiFosSign.SignAuth(sign_secret, baseController.DynamicStr)) { if (!ConfigManage.AppSettings <bool>("AppSettings:IsDebugModel")) { context.Result = APIResponse.GetResult(StateCode.State_5); } else { context.Result = APIResponse.GetResult(StateCode.State_5); } return; } } }
/// <summary> /// 登录 /// </summary> /// <returns></returns> private async Task <IActionResult> Func200() { return(await Task.Run(() => { try { return APIResponse.GetResult(StateCode.State_200); } catch (Exception ex) { ServiceIoc.Get <APILogsService>().Save("登录接口==>" + ex.ToString()); return APIResponse.GetResult(StateCode.State_500); } })); }
/// <summary> /// 登录 /// </summary> /// <returns></returns> private async Task <IActionResult> Func100() { return(await Task.Run(() => { try { //用户 string login_name = Dynamic.Data.LoginName.ToString(); //密码 string pass_word = Dynamic.Data.Password.ToString(); //是否登录 User user = ServiceIoc.Get <UserService>().Login(login_name, pass_word, HttpContext.GetClientIp(), Sign); if (user.login_code == StateCode.State_200) { var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.login_name), new Claim("UserId", user.id.ToString()), new Claim("HeadImg", user.head_img ?? "") }; ClaimsIdentity identity = new ClaimsIdentity(new GenericIdentity(user.login_name, "TokenAuth"), claims); var now = DateTime.UtcNow; var ex = now + TimeSpan.FromMinutes(60); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(ConfigManage.AppSettings <string>("Jwt:Key"))); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);//加密方式 var token = new SecurityTokenDescriptor { //Jwt token 的签发者 Issuer = ConfigManage.AppSettings <string>("AppSettings:DomainApi"), //Jwt token 的接收者 Audience = ConfigManage.AppSettings <string>("AppSettings:DomainApi"), IssuedAt = now, Expires = ex, SigningCredentials = creds, Subject = identity }; //基于Jwt身份认证 //var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme)); //签发一个加密后的用户信息凭证,用来标识用户的身份 HttpContext.SignInAsync(JwtBearerDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); var tokenHandler = new JwtSecurityTokenHandler(); return APIResponse.GetResult(user.login_code, new { token = tokenHandler.CreateEncodedJwt(token), sid = user.id, name = user.login_name, auth_time = new DateTimeOffset(now).ToUnixTimeSeconds(), expires_at = new DateTimeOffset(ex).ToUnixTimeSeconds() }); } return APIResponse.GetResult(user.login_code); } catch (Exception ex) { ServiceIoc.Get <APILogsService>().Save("登录接口==>" + ex.ToString()); return APIResponse.GetResult(StateCode.State_500); } })); }