|
@@ -1,41 +1,47 @@
|
|
|
-using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.AspNetCore.Mvc.Authorization;
|
|
|
+using Microsoft.AspNetCore.Mvc.Filters;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
-using WebAPIBase.Utils.Jwt;
|
|
|
using WebAPIBase.Utils;
|
|
|
-using Microsoft.AspNetCore.Mvc;
|
|
|
+using WebAPIBase.Utils.Jwt;
|
|
|
|
|
|
namespace WebAPIBase.API.Filter
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// 过滤器实现通用token验证
|
|
|
+ /// 令牌授权,登录验证
|
|
|
/// </summary>
|
|
|
- public class TokenFilter : Attribute, IActionFilter
|
|
|
+ public class TokenAuthorize : Attribute, IAuthorizationFilter
|
|
|
{
|
|
|
private ITokenHelper tokenHelper;
|
|
|
/// <summary>
|
|
|
/// 滤器实现通用token验证
|
|
|
/// </summary>
|
|
|
/// <param name="_tokenHelper"></param>
|
|
|
- public TokenFilter(ITokenHelper _tokenHelper) //通过依赖注入得到数据访问层实例
|
|
|
+ public TokenAuthorize(ITokenHelper _tokenHelper) //通过依赖注入得到数据访问层实例
|
|
|
{
|
|
|
tokenHelper = _tokenHelper;
|
|
|
}
|
|
|
-
|
|
|
- public void OnActionExecuted(ActionExecutedContext context)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
- public void OnActionExecuting(ActionExecutingContext context)
|
|
|
+ public void OnAuthorization(AuthorizationFilterContext context)
|
|
|
{
|
|
|
+
|
|
|
ApiResponse ret = new ApiResponse();
|
|
|
+ if(HasAllowAnonymous(context))
|
|
|
+ {
|
|
|
+ ret.IsSuccess = true;
|
|
|
+ ret.Code = 200;
|
|
|
+ ret.ErrMsg = "";
|
|
|
+ return;
|
|
|
+ }
|
|
|
//获取token
|
|
|
//object tokenobj = context.ActionArguments["token"];//前端地址栏参数传参
|
|
|
- object tokenobj = context.HttpContext.Request.Headers["X-Token"].ToString();//前端写在header里面获取的
|
|
|
- if (tokenobj == null)
|
|
|
+ var token = context.HttpContext.Request.Headers["X-Token"].ToString();//前端写在header里面获取的
|
|
|
+ if (token.IsNullOrEmpty())
|
|
|
{
|
|
|
ret.IsSuccess = false;
|
|
|
ret.Code = 201;
|
|
@@ -44,7 +50,6 @@ namespace WebAPIBase.API.Filter
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- string token = tokenobj.ToString();
|
|
|
|
|
|
string userId = "";
|
|
|
//验证jwt,同时取出来jwt里边的用户ID
|
|
@@ -70,5 +75,32 @@ namespace WebAPIBase.API.Filter
|
|
|
//context.ActionArguments.Add("userId", Convert.ToInt32(userId));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 用于判断Action有没有AllowAnonymous标签
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private bool HasAllowAnonymous(AuthorizationFilterContext context)
|
|
|
+ {
|
|
|
+ var filters = context.Filters;
|
|
|
+ for (var i = 0; i < filters.Count; i++)
|
|
|
+ {
|
|
|
+ if (filters[i] is IAllowAnonymousFilter)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var endpoint = context.HttpContext.GetEndpoint();
|
|
|
+ if (endpoint?.Metadata?.GetMetadata<IAllowAnonymous>() != null)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|