update 0.1.6 , change checkAuth Attr
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@@ -56,12 +57,33 @@ namespace YYApi.Helpers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class CheckAuthAttribute : ActionFilterAttribute
|
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)
|
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);
|
throw new ApiException(401);
|
||||||
}
|
}
|
||||||
|
//检测用户类型
|
||||||
|
if (!Types.Contains(loginInfo.Type))
|
||||||
|
{
|
||||||
|
throw new ApiException(403);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +104,7 @@ namespace YYApi.Helpers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string SetId(int id, string type = "user")
|
public string Set(int id, string type = "user")
|
||||||
{
|
{
|
||||||
var uuid = GetUUID();
|
var uuid = GetUUID();
|
||||||
var loginItem = new LoginInfoItem
|
var loginItem = new LoginInfoItem
|
||||||
@@ -99,4 +121,4 @@ namespace YYApi.Helpers
|
|||||||
return Guid.NewGuid().ToString();
|
return Guid.NewGuid().ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取登录用户信息
|
/// 获取登录用户信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected User LoginInfo => (User) HttpContext.Items["LoginInfo"];
|
protected LoginInfoItem LoginInfo => (LoginInfoItem) HttpContext.Items["LoginInfo"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -40,7 +40,7 @@ namespace Controllers
|
|||||||
#Startup.cs
|
#Startup.cs
|
||||||
service.AddAuth();
|
service.AddAuth();
|
||||||
```
|
```
|
||||||
添加时候, 可以从DI中获取 Auth 类,调用 Auth.SetId(int) 将用户ID和生成的Token绑定,本方法返回缓存中的Key名称
|
添加时候, 可以从DI中获取 Auth 类,调用 Auth.Set(int,string) 将用户ID/类型和生成的Token绑定,本方法返回缓存中的Key名称
|
||||||
|
|
||||||
```C#
|
```C#
|
||||||
#Startup.cs
|
#Startup.cs
|
||||||
|
|||||||
+1
-6
@@ -4,15 +4,13 @@
|
|||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<PackOnBuild>true</PackOnBuild>
|
<PackOnBuild>true</PackOnBuild>
|
||||||
<Version>0.1.3</Version>
|
<Version>0.1.6</Version>
|
||||||
<Authors>xRain@YYTech</Authors>
|
<Authors>xRain@YYTech</Authors>
|
||||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||||
<PackageId>YY-Tech.YYApi</PackageId>
|
<PackageId>YY-Tech.YYApi</PackageId>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
<ReleaseVersion>0.1.5</ReleaseVersion>
|
|
||||||
<SynchReleaseVersion>false</SynchReleaseVersion>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -20,9 +18,6 @@
|
|||||||
<Folder Include="Communications\" />
|
<Folder Include="Communications\" />
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Controllers\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Content Remove="Properties\launchSettings.json" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc5" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc5" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc5" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc5" />
|
||||||
|
|||||||
Reference in New Issue
Block a user