update 0.1.6 , change checkAuth Attr

This commit is contained in:
2020-01-06 13:08:26 +08:00
parent d88ee5f50d
commit c4e852a0df
3 changed files with 28 additions and 11 deletions
+25 -3
View File
@@ -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
/// </summary>
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
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
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();
}
}
}
}