You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
7.2 KiB
205 lines
7.2 KiB
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.Cloudcar.Admin.AspNetCore.Entitys;
|
|
using Znyc.Cloudcar.Admin.AspNetCore.Mvc;
|
|
using Znyc.Cloudcar.Admin.Commons.Entitys;
|
|
using Znyc.Cloudcar.Admin.Commons.Json;
|
|
using Znyc.Cloudcar.Admin.Commons.Log;
|
|
using Znyc.Cloudcar.Admin.Commons.Options;
|
|
using Znyc.Cloudcar.Admin.Security.IServices;
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
namespace Znyc.Cloudcar.Admin.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Token令牌接口控制器
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class TokenController : ControllerBase
|
|
{
|
|
private readonly IAPPService _iAPPService;
|
|
private readonly JwtOption _jwtModel;
|
|
private readonly IAdminUserService userService;
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="iAPPService"></param>
|
|
/// <param name="_userService"></param>
|
|
/// <param name="jwtModel"></param>
|
|
public TokenController(IAPPService iAPPService, IAdminUserService _userService, JwtOption jwtModel)
|
|
{
|
|
if (iAPPService == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(iAPPService));
|
|
}
|
|
|
|
_iAPPService = iAPPService;
|
|
userService = _userService;
|
|
_jwtModel = jwtModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据应用信息获得token令牌
|
|
/// </summary>
|
|
/// <param name="grant_type">获取access_token填写client_credential</param>
|
|
/// <param name="appid">应用唯一凭证,应用AppId</param>
|
|
/// <param name="secret">应用密钥AppSecret</param>
|
|
/// <returns></returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证token的合法性。
|
|
/// </summary>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("CheckToken")]
|
|
[AllowAnonymous]
|
|
public IActionResult CheckToken(string token)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
TokenProvider tokenProvider = new TokenProvider(_jwtModel);
|
|
result = tokenProvider.ValidateToken(token);
|
|
return ToJsonContent(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刷新token。
|
|
/// </summary>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("RefreshToken")]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> 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<Claim> claimlist = jwtToken?.Payload.Claims as List<Claim>;
|
|
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<Claim> claimlist = jwtToken?.Payload.Claims as List<Claim>;
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 把object对象转换为ContentResult
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Route("api/ToJsonContent")]
|
|
protected IActionResult ToJsonContent(object obj)
|
|
{
|
|
string result = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
|
return Content(obj.ToJson());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取CosToken
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[AllowAnonymous]
|
|
[Route("api/GetCosToken")]
|
|
|
|
public CommonResult GetCosToken()
|
|
{
|
|
return _iAPPService.GetCosToken();
|
|
}
|
|
|
|
}
|
|
}
|