using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Syc.Authorize.JwtBearer; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Microsoft.Extensions.DependencyInjection { public static class JwtBearerExtensions { /// /// 未授权 /// /// /// internal static async Task UnauthorizedAsync(this HttpResponse httpResponse) { httpResponse.ContentType = "application/json"; var errorMsgResponse = GetResponse(StatusCodes.Status401Unauthorized, "Unauthorized"); await httpResponse.WriteAsJsonAsync(errorMsgResponse); } /// /// 权限不足 /// /// internal static async Task ForbiddenAsync(this HttpResponse httpResponse) { httpResponse.ContentType = "application/json"; var errorMsgResponse = GetResponse(StatusCodes.Status403Forbidden, "Forbidden"); await httpResponse.WriteAsJsonAsync(errorMsgResponse); } private static JwtBearerErrorMsgResponse GetResponse(int statusCode, string msg) => new JwtBearerErrorMsgResponse() { Code = statusCode, Data = null, Message = msg }; public static IApplicationBuilder UseAppAuthentication(this IApplicationBuilder app) { app.UseAuthentication(); app.UseAuthorization(); return app; } } }