修复 [Authorize]时异常问题

优化 接口异常捕获
新增 dockerFile文件
This commit is contained in:
小肥羊 2024-11-25 17:45:52 +08:00
parent b442e4f336
commit 8e0cc81283
12 changed files with 97 additions and 21 deletions

View File

@ -10,12 +10,14 @@
"Host": "192.168.2.21:10107", "Host": "192.168.2.21:10107",
"NickName": "", "NickName": "",
"IIsFireUrl": null, "IIsFireUrl": null,
"DockerFireUrl": "", "DockerFireUrl": "https://videoanalysis.w.23544.com:8843/",
"WindowsServiceFireUrl": null, "WindowsServiceFireUrl": null,
"LinuxServiceFireUrl": null "LinuxServiceFireUrl": null
} }
], ],
"IgnoreList": [], "IgnoreList": [
"ffmpeg.exe"
],
"WindowsBackUpIgnoreList": [] "WindowsBackUpIgnoreList": []
} }
], ],

View File

@ -3,8 +3,8 @@
<PageTitle>Error</PageTitle> <PageTitle>Error</PageTitle>
<h1 class="text-danger">Error.</h1> <h1 class="text-danger">错误页面.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2> <h2 class="text-danger">处理您的请求时出错。</h2>
@if (ShowRequestId) @if (ShowRequestId)
{ {
@ -13,15 +13,15 @@
</p> </p>
} }
<h3>Development Mode</h3> <h3>开发模式</h3>
<p> <p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred. 切换到<strong>Development</strong>环境将显示有关发生的错误的更多详细信息。
</p> </p>
<p> <p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> <strong> 不应为已部署的应用程序启用开发环境。</strong>
It can result in displaying sensitive information from exceptions to end users. 它可能导致向最终用户显示来自异常的敏感信息。
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> 对于本地调试,通过将 <strong>ASPNETCORE_ENVIRONMENT</strong> 环境变量设置为 <strong>Development</strong> 来启用 <strong> 开发 </strong> 环境
and restarting the app. 并重新启动应用程序。
</p> </p>
@code{ @code{

View File

@ -62,7 +62,14 @@ namespace VideoAnalysisRazor.Layouts
public async Task<bool> CheckLogin() public async Task<bool> CheckLogin()
{ {
return (await session.GetAsync<bool>("Login")).Value; try
{
return (await session.GetAsync<bool>("Login")).Value;
}
catch
{
return false;
}
} }
void Reload() void Reload()
{ {

View File

@ -1,5 +1,4 @@
using AntDesign.TableModels; using AntDesign.TableModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using SqlSugar; using SqlSugar;
using System.Linq.Expressions; using System.Linq.Expressions;
@ -8,7 +7,6 @@ using VideoAnalysisCore.Model;
namespace Learn.VideoAnalysis.Components.Pages namespace Learn.VideoAnalysis.Components.Pages
{ {
[Authorize]
public partial class EvaluationProject : ComponentBase public partial class EvaluationProject : ComponentBase
{ {

View File

@ -23,7 +23,7 @@
<div class="container"> <div class="container">
<div class="form"> <div class="form">
<h2>登录 视频分析平台</h2> <h2>登录 视频分析平台</h2>
<form> <div class="cform">
<div class="inputBox"> <div class="inputBox">
<input type="text" placeholder="账号" @bind="InputAccount"> <input type="text" placeholder="账号" @bind="InputAccount">
</div> </div>
@ -47,7 +47,7 @@
注册 注册
</a> </a>
</p> *@ </p> *@
</form> </div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,8 +1,12 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. #See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM dotnet/aspnet:8.0 FROM dotnet/aspnet:8.0
RUN ["apt-get", "--assume-yes", "update"]
RUN ["apt-get", "--assume-yes", "install", "ffmpeg"]
WORKDIR /app WORKDIR /app
EXPOSE 80 EXPOSE 9040
COPY . . COPY . .
#设置时间为中国上海 环境为开发环境 #设置时间为中国上海 环境为开发环境
@ -12,5 +16,6 @@ ENV TZ=Asia/Shanghai
#RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ sid main contrib non-free" > /etc/apt/sources.list #RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ sid main contrib non-free" > /etc/apt/sources.list
#RUN apt-get update && apt-get install -y apt-transport-https ca-certificates apt-utils libgdiplus libc6-dev && apt-get install -y libfreetype6 && apt-get install -y libfontconfig1 && apt-get install -y fontconfig #RUN apt-get update && apt-get install -y apt-transport-https ca-certificates apt-utils libgdiplus libc6-dev && apt-get install -y libfreetype6 && apt-get install -y libfontconfig1 && apt-get install -y fontconfig
# #
ENV ASPNETCORE_URLS=http://+:9040
ENTRYPOINT ["dotnet", "Learn.VideoAnalysis.dll"] ENTRYPOINT ["dotnet", "Learn.VideoAnalysis.dll"]

View File

@ -55,6 +55,10 @@ namespace Learn.VideoAnalysis
RedisExpand.Init(); RedisExpand.Init();
Speaker.Init(); Speaker.Init();
//SenseVoice.Init(); //SenseVoice.Init();
builder.Services.AddControllersWithViews(options =>
{
options.Filters.Add(typeof(ExceptionFilter));
});
builder.Services.AddScoped(sp => builder.Services.AddScoped(sp =>
@ -94,8 +98,11 @@ namespace Learn.VideoAnalysis
app.UseSwaggerUI(); app.UseSwaggerUI();
app.UseExceptionHandler("/Error"); app.UseExceptionHandler("/Error");
} }
//else
//{
// app.UseExceptionHandler("/Login");
//}
app.UseAuthorization();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseAntiforgery(); app.UseAntiforgery();

View File

@ -7,7 +7,7 @@
}, },
"AppConfig": { "AppConfig": {
"Redis": { "Redis": {
"ConnectionString": "10.255.255.3,password=qwe123!@#,defaultDatabase=10" "ConnectionString": "10.255.255.3,password=qwe123!@#,defaultDatabase=3"
}, },
"FFmpeg": { "FFmpeg": {
" TimeSlice": 600 " TimeSlice": 600

View File

@ -5,6 +5,7 @@ using VideoAnalysisCore.Common;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType; using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Xml.Linq; using System.Xml.Linq;
using System.Runtime.InteropServices;
namespace VideoAnalysisCore.AICore.FFMPGE namespace VideoAnalysisCore.AICore.FFMPGE
{ {
@ -16,7 +17,9 @@ namespace VideoAnalysisCore.AICore.FFMPGE
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public static string FFmpegPath = Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe"); public static string FFmpegPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? $"/usr/bin/ffmpeg"
: Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe");
/// <summary> /// <summary>
/// 音频转码为 wav_16k /// 音频转码为 wav_16k
/// </summary> /// </summary>

View File

@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace VideoAnalysisCore.Common
{
public class ExceptionFilter : IAsyncExceptionFilter
{
readonly NavigationManager nm;
public ExceptionFilter(NavigationManager nm)
{
this.nm = nm;
}
public async Task OnExceptionAsync(ExceptionContext context)
{
if (context.Exception is InvalidOperationException)
{
nm.NavigateTo("/Login");
}
else
{
// 创建一个包含错误信息的对象
var errorObject = new
{
ErrorMessage = context.Exception.Message,
StackTrace = context.Exception.StackTrace,
};
// 将错误对象序列化为JSON格式
var json = JsonSerializer.Serialize(errorObject);
// 设置响应内容类型为JSON
context.HttpContext.Response.ContentType = "application/json";
// 设置状态码
context.HttpContext.Response.StatusCode = 500;
// 将JSON数据写入响应体
await context.HttpContext.Response.WriteAsync(json);
}
}
}
}

View File

@ -92,7 +92,11 @@ namespace VideoAnalysisCore.Common
Console.WriteLine("初始化 redis"); Console.WriteLine("初始化 redis");
Redis.Serialize = obj => System.Text.Json.JsonSerializer.Serialize(obj); Redis.Serialize = obj => System.Text.Json.JsonSerializer.Serialize(obj);
Redis.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type); Redis.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type);
InitChannel(); Task.Run(() =>
{
Task.Delay(1000 * 5);
InitChannel();
});
} }
/// <summary> /// <summary>

View File

@ -15,7 +15,7 @@
<ItemGroup> <ItemGroup>
<Content Include="AICore\_Static\ffmpeg.exe"> <Content Include="AICore\_Static\ffmpeg.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content> </Content>
<Content Include="AICore\_Static\sherpa-onnx-pyannote-segmentation-3-0\model.int8.onnx"> <Content Include="AICore\_Static\sherpa-onnx-pyannote-segmentation-3-0\model.int8.onnx">
<CopyToOutputDirectory>Never</CopyToOutputDirectory> <CopyToOutputDirectory>Never</CopyToOutputDirectory>
@ -32,6 +32,7 @@
<PackageReference Include="FreeRedis" Version="1.3.2" /> <PackageReference Include="FreeRedis" Version="1.3.2" />
<PackageReference Include="Downloader" Version="3.2.1" /> <PackageReference Include="Downloader" Version="3.2.1" />
<PackageReference Include="Mapster" Version="7.4.1-pre01" /> <PackageReference Include="Mapster" Version="7.4.1-pre01" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />