55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
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
|
|
{
|
|
|
|
/// <summary>
|
|
/// 未授权
|
|
/// </summary>
|
|
/// <param name="httpResponse"></param>
|
|
/// <returns></returns>
|
|
internal static async Task UnauthorizedAsync(this HttpResponse httpResponse)
|
|
{
|
|
httpResponse.ContentType = "application/json";
|
|
var errorMsgResponse = GetResponse(StatusCodes.Status401Unauthorized, "Unauthorized");
|
|
await httpResponse.WriteAsJsonAsync(errorMsgResponse);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 权限不足
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|