Compare commits

...
3 Commits
Author SHA1 Message Date
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
xrain b2a6e5f40f make storage bucket public 2022-08-17 20:26:25 +08:00
3 changed files with 25 additions and 12 deletions
+12 -6
View File
@@ -1,4 +1,4 @@
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
@@ -19,13 +19,16 @@ namespace SimApi.Attributes
//默认是user登录类型 //默认是user登录类型
public SimApiAuthAttribute() public SimApiAuthAttribute()
{ {
Types = new[] { "user" }; Types = new[]
{
"user"
};
} }
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type) public SimApiAuthAttribute(string type)
{ {
Types = new[] { type }; Types = type.Split(",");
} }
//设定特定类型的检测 //设定特定类型的检测
@@ -37,7 +40,10 @@ namespace SimApi.Attributes
//只检测一种用户类型的快捷方式 //只检测一种用户类型的快捷方式
public SimApiAuthAttribute(string type, string url) public SimApiAuthAttribute(string type, string url)
{ {
Types = new[] { type }; Types = new[]
{
type
};
new HttpPostAttribute(url); new HttpPostAttribute(url);
} }
@@ -50,10 +56,10 @@ namespace SimApi.Attributes
throw new SimApiException(401); throw new SimApiException(401);
} }
//检测用户类型 //检测用户类型
if (Types.Intersect(loginInfo.Type).Count() == 0) if (!Types.Intersect(loginInfo.Type).Any())
{ {
throw new SimApiException(403); throw new SimApiException(403);
} }
} }
} }
} }
+2 -2
View File
@@ -15,7 +15,7 @@ namespace SimApi.Helpers
private string ServeUrl { get; } private string ServeUrl { get; }
private string Bucket { get; } public string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; } private IHttpContextAccessor HttpContextAccessor { get; }
@@ -49,7 +49,7 @@ namespace SimApi.Helpers
} }
Mc = mcb.Build(); Mc = mcb.Build();
bool found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result; var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
if (!found) if (!found)
{ {
Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait(); Mc.MakeBucketAsync(new MakeBucketArgs().WithBucket(Bucket)).Wait();
+11 -4
View File
@@ -14,6 +14,7 @@ namespace SimApi.Middlewares
public class SimApiExceptionMiddleware public class SimApiExceptionMiddleware
{ {
private RequestDelegate Next { get; } private RequestDelegate Next { get; }
private ILogger<SimApiExceptionMiddleware> Log { get; } private ILogger<SimApiExceptionMiddleware> Log { get; }
public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log) public SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExceptionMiddleware> log)
@@ -35,7 +36,10 @@ namespace SimApi.Middlewares
await Next(context); await Next(context);
if (context.Response.StatusCode != 200) 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); throw new SimApiException(context.Response.StatusCode);
} }
@@ -70,9 +74,12 @@ namespace SimApi.Middlewares
/// <param name="response"></param> /// <param name="response"></param>
private void ErrorResponse(HttpContext context, SimApiBaseResponse response) private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{ {
context.Response.StatusCode = 200; if (!context.Response.HasStarted)
context.Response.Headers.Add("Content-Type", "application/json"); {
context.Response.WriteAsync(response.ToString()); context.Response.StatusCode = 200;
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.WriteAsync(response.ToString());
}
} }
} }
} }