Compare commits

...
3 Commits
Author SHA1 Message Date
xrain b5e4d57772 update package 2023-02-05 19:43:39 +08:00
xrain 239cd8bcbb fix error when exception response has started 2022-09-14 04:56:21 +08:00
xrain 62ddd6698e now simapiauth can use , for role 2022-09-02 09:47:41 +08:00
3 changed files with 27 additions and 14 deletions
+12 -6
View File
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
@@ -19,13 +19,16 @@ namespace SimApi.Attributes
//默认是user登录类型
public SimApiAuthAttribute()
{
Types = new[] { "user" };
Types = new[]
{
"user"
};
}
//只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type)
{
Types = new[] { type };
Types = type.Split(",");
}
//设定特定类型的检测
@@ -37,7 +40,10 @@ namespace SimApi.Attributes
//只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url)
{
Types = new[] { type };
Types = new[]
{
type
};
new HttpPostAttribute(url);
}
@@ -50,10 +56,10 @@ namespace SimApi.Attributes
throw new SimApiException(401);
}
//检测用户类型
if (Types.Intersect(loginInfo.Type).Count() == 0)
if (!Types.Intersect(loginInfo.Type).Any())
{
throw new SimApiException(403);
}
}
}
}
}
+11 -4
View File
@@ -14,6 +14,7 @@ namespace SimApi.Middlewares
public class SimApiExceptionMiddleware
{
private RequestDelegate Next { get; }
private ILogger<SimApiExceptionMiddleware> Log { get; }
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
@@ -35,7 +36,10 @@ namespace SimApi.Middlewares
await Next(context);
if (context.Response.StatusCode != 200)
{
if (!new[] {301, 302}.Contains(context.Response.StatusCode))
if (!new[]
{
301, 302
}.Contains(context.Response.StatusCode))
{
throw new SimApiException(context.Response.StatusCode);
}
@@ -70,9 +74,12 @@ namespace SimApi.Middlewares
/// <param name="response"></param>
private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString());
if (!context.Response.HasStarted)
{
context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString());
}
}
}
}
+4 -4
View File
@@ -14,7 +14,7 @@
<SynchReleaseVersion>false</SynchReleaseVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>5.0.2</PackageVersion>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
@@ -27,9 +27,9 @@
<Folder Include="Configurations\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Minio" Version="4.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.3.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" />
<PackageReference Include="Minio" Version="4.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>