public override void OnActionExecuting(ActionExecutingContext requestContext) { var result = new ApiResult <string>(); result.Code = Code.UNAUTHORIZED; result.Message = "授权失败"; HttpContext httpContext = requestContext.HttpContext; if (requestContext.Filters.Any(f => f is IAllowAnonymousFilter)) { return; } var key = httpContext.Request.Headers.Keys; if (httpContext.Request.Headers.ContainsKey(_token)) { try { var tokenHeader = httpContext.Request.Headers[_token].ToString(); if (!string.IsNullOrWhiteSpace(tokenHeader)) { //解析token var info = JwtTokenAuthorize.ReadToken(tokenHeader); if (info != null) { //获取用户是否存在 //判断是否有权限访问 //增加缓存 //返回结果 } } } catch (Exception) { // Logger.Error($"{DateTime.Now} middleware wrong:{ex.Message}"); } } base.OnActionExecuting(requestContext); }
public Task <ApiResult <LoginResult> > Login([FromBody] LoginVm model) { var apiResult = new ApiResult <LoginResult>(); var data = new LoginResult(); apiResult.Code = Code.OK; var isok = _userService.SignIn(model.UserName, model.PassWord); string token = JwtTokenAuthorize.CreateToken(new JwtTokenVm() { UserName = model.UserName, Expires = DateTime.Now.AddDays(11), Role = new string[] { "admin", "people" } }); //var sdas = JwtTokenAuthorize.ReadToken(token); //var user = _userService.Authenticate(model.UserName, model.PassWord); //if (user == null) // return BadRequest(new {message = "Username or password is incorrect"}); data.Userid = Guid.NewGuid().ToString(); data.Username = "******"; data.Mobile = "guojiaqiu"; data.Email = "15810803044"; data.Role = string.Join(",", new string[] { "admin", "people" }); data.Token = token; apiResult.Data = data; return(Task.FromResult(apiResult)); }