update 0.1.6 , change checkAuth Attr
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Controllers
|
||||
/// <summary>
|
||||
/// 获取登录用户信息
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
+1
-6
@@ -4,15 +4,13 @@
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<PackOnBuild>true</PackOnBuild>
|
||||
<Version>0.1.3</Version>
|
||||
<Version>0.1.6</Version>
|
||||
<Authors>xRain@YYTech</Authors>
|
||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||
<PackageId>YY-Tech.YYApi</PackageId>
|
||||
<IsPackable>true</IsPackable>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<ReleaseVersion>0.1.5</ReleaseVersion>
|
||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -20,9 +18,6 @@
|
||||
<Folder Include="Communications\" />
|
||||
<Folder Include="Controllers\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Remove="Properties\launchSettings.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc5" />
|
||||
|
||||
Reference in New Issue
Block a user