From c4e852a0dfc4892cc3fdc30f9db3f387d5f93f52 Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 6 Jan 2020 13:08:26 +0800 Subject: [PATCH] update 0.1.6 , change checkAuth Attr --- Helpers/AuthMiddleware.cs | 28 +++++++++++++++++++++++++--- README.md | 4 ++-- YYApi.csproj | 7 +------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Helpers/AuthMiddleware.cs b/Helpers/AuthMiddleware.cs index 604d60c..c6c9d5f 100644 --- a/Helpers/AuthMiddleware.cs +++ b/Helpers/AuthMiddleware.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -56,12 +57,33 @@ namespace YYApi.Helpers /// public class CheckAuthAttribute : ActionFilterAttribute { + private string[] Types { get; } + + //默认是user登录类型 + public CheckAuthAttribute() + { + Types = new[] { "user" }; + } + + //设定特定类型的检测 + public CheckAuthAttribute(string[] types) + { + Types = types; + } + public override void OnActionExecuting(ActionExecutingContext context) { - if (context.HttpContext.Items["LoginId"] == null) + var loginInfo = (LoginInfoItem)context.HttpContext.Items["LoginInfo"]; + //检测是否登录 + if (loginInfo == null) { throw new ApiException(401); } + //检测用户类型 + if (!Types.Contains(loginInfo.Type)) + { + throw new ApiException(403); + } } } @@ -82,7 +104,7 @@ namespace YYApi.Helpers /// /// /// - public string SetId(int id, string type = "user") + public string Set(int id, string type = "user") { var uuid = GetUUID(); var loginItem = new LoginInfoItem @@ -99,4 +121,4 @@ namespace YYApi.Helpers return Guid.NewGuid().ToString(); } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 7323216..a1ccacc 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ namespace Controllers /// /// 获取登录用户信息 /// - protected User LoginInfo => (User) HttpContext.Items["LoginInfo"]; + protected LoginInfoItem LoginInfo => (LoginInfoItem) HttpContext.Items["LoginInfo"]; } } ``` @@ -40,7 +40,7 @@ namespace Controllers #Startup.cs service.AddAuth(); ``` -添加时候, 可以从DI中获取 Auth 类,调用 Auth.SetId(int) 将用户ID和生成的Token绑定,本方法返回缓存中的Key名称 +添加时候, 可以从DI中获取 Auth 类,调用 Auth.Set(int,string) 将用户ID/类型和生成的Token绑定,本方法返回缓存中的Key名称 ```C# #Startup.cs diff --git a/YYApi.csproj b/YYApi.csproj index 0404c7f..e48dd10 100644 --- a/YYApi.csproj +++ b/YYApi.csproj @@ -4,15 +4,13 @@ netcoreapp3.1 Library true - 0.1.3 + 0.1.6 xRain@YYTech AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库 YY-Tech.YYApi true snupkg true - 0.1.5 - false @@ -20,9 +18,6 @@ - - -