using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Threading.Tasks; using Znyc.Recruitment.Admin.AspNetCore.Entitys; using Znyc.Recruitment.Admin.AspNetCore.Mvc; using Znyc.Recruitment.Admin.Commons.Entitys; using Znyc.Recruitment.Admin.Commons.Json; using Znyc.Recruitment.Admin.Commons.Log; using Znyc.Recruitment.Admin.Commons.Options; using Znyc.Recruitment.Admin.Security.IServices; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace Znyc.Recruitment.Admin.WebApi.Controllers { /// /// Token令牌接口控制器 /// [Route("api/[controller]")] [ApiController] public class TokenController : ControllerBase { private readonly IAPPService _iAPPService; private readonly JwtOption _jwtModel; private readonly IAdminUserService userService; /// /// 构造函数 /// /// /// /// public TokenController(IAPPService iAPPService, IAdminUserService _userService, JwtOption jwtModel) { if (iAPPService == null) { throw new ArgumentNullException(nameof(iAPPService)); } _iAPPService = iAPPService; userService = _userService; _jwtModel = jwtModel; } /// /// 根据应用信息获得token令牌 /// /// 获取access_token填写client_credential /// 应用唯一凭证,应用AppId /// 应用密钥AppSecret /// [HttpGet] [AllowAnonymous] public IActionResult Get(string grant_type, string appid, string secret) { CommonResult result = new CommonResult(); if (!grant_type.Equals(GrantType.ClientCredentials)) { result.ErrCode = "40003"; result.ErrMsg = ErrCode.err40003; return ToJsonContent(result); } if (string.IsNullOrEmpty(grant_type)) { result.ErrCode = "40003"; result.ErrMsg = ErrCode.err40003; return ToJsonContent(result); } string strHost = Request.Host.ToString(); Security.Entitys.APPEntity app = _iAPPService.GetAPP(appid, secret); Console.WriteLine(app); if (app == null) { result.ErrCode = "40001"; result.ErrMsg = ErrCode.err40001; } else { TokenProvider tokenProvider = new TokenProvider(_jwtModel); TokenResult tokenResult = tokenProvider.GenerateToken(grant_type, appid, secret); result.ResData = tokenResult; result.ErrCode = "0"; return ToJsonContent(result); } return ToJsonContent(result); } /// /// 验证token的合法性。 /// /// /// [HttpGet("CheckToken")] [AllowAnonymous] public IActionResult CheckToken(string token) { CommonResult result = new CommonResult(); TokenProvider tokenProvider = new TokenProvider(_jwtModel); result = tokenProvider.ValidateToken(token); return ToJsonContent(result); } /// /// 刷新token。 /// /// /// [HttpGet("RefreshToken")] [AllowAnonymous] public async Task RefreshToken(string token) { CommonResult result = new CommonResult(); TokenProvider tokenProvider = new TokenProvider(_jwtModel); if (!string.IsNullOrEmpty(token)) { JwtSecurityToken jwtToken = new JwtSecurityTokenHandler().ReadJwtToken(token); #if DEBUG Log4NetHelper.Debug(jwtToken.ToJson()); #endif if (jwtToken != null) { //根据应用获取token if (jwtToken.Subject == GrantType.ClientCredentials) { TokenResult tresult = new TokenResult(); List claimlist = jwtToken?.Payload.Claims as List; string strHost = Request.Host.ToString(); Security.Entitys.APPEntity app = _iAPPService.GetAPP(claimlist[0].Value); if (app == null) { result.ErrCode = "40001"; result.ErrMsg = ErrCode.err40001; } else { TokenResult tokenResult = tokenProvider.GenerateToken(GrantType.ClientCredentials, app.AppId, app.AppSecret); result.ResData = tokenResult; result.ErrCode = "0"; result.Success = true; } } // 用户账号密码登录获取token类型 if (jwtToken.Subject == GrantType.Password) { List claimlist = jwtToken?.Payload.Claims as List; Security.Entitys.AdminUserEntity user = await userService.GetByUserName(claimlist[2].Value); TokenResult tokenResult = tokenProvider.LoginToken(user, claimlist[0].Value); result.ResData = tokenResult; result.ErrCode = "0"; result.Success = true; } } else { result.ErrMsg = ErrCode.err40004; result.ErrCode = "40004"; } } else { result.ErrMsg = ErrCode.err40004; result.ErrCode = "40004"; } return ToJsonContent(result); } /// /// 把object对象转换为ContentResult /// /// /// [HttpPost] [Route("api/ToJsonContent")] protected IActionResult ToJsonContent(object obj) { string result = JsonConvert.SerializeObject(obj, Formatting.Indented); return Content(obj.ToJson()); } /// /// 获取CosToken /// /// [HttpGet] [AllowAnonymous] [Route("api/GetCosToken")] public CommonResult GetCosToken() { return _iAPPService.GetCosToken(); } } }