在*****.****.Host的PublicHealthHttpApiHostModule文件中添加以下内容
context.Services.ConfigureApplicationCookie(options =>
options.Events.OnRedirectToLogin = httpContext =>
{
httpContext.Response.ContentType = "application/json;charset=utf-8";
httpContext.Response.Headers["Location"] = httpContext.RedirectUri;
httpContext.Response.StatusCode = 403;
//private readonly IJsonSerializer _jsonSerializer;
using (var scope = context.Services.BuildServiceProvider())
{
var result = new RemoteServiceErrorResponse(
new RemoteServiceErrorInfo { Code = httpContext.Response.StatusCode.ToString(), Message = "没有权限,你乱点什么?" });
httpContext.Response.WriteAsync(scope.GetRequiredService<IJsonSerializer>().Serialize(result));
}
return System.Threading.Tasks.Task.CompletedTask;
});
测试结果:
如果没有附加token