| | |
| | | using Microsoft.IdentityModel.Tokens; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.IdentityModel.Tokens.Jwt; |
| | | using System.Linq; |
| | | using System.Security.Claims; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core.Const; |
| | | using WIDESEAWCS_Core.Extensions; |
| | | using WIDESEAWCS_Core.Helper; |
| | | |
| | | namespace WIDESEAWCS_Core.Authorization |
| | |
| | | public static void AddAuthorizationSetup(this IServiceCollection services) |
| | | { |
| | | if (services == null) throw new ArgumentNullException(nameof(services)); |
| | | |
| | | |
| | | services.AddAuthentication(options => |
| | | { |
| | | options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; |
| | |
| | | context.Response.StatusCode = 401; |
| | | context.Response.WriteAsync(new { message = "授权未通过", status = false, code = 401 }.Serialize()); |
| | | return Task.CompletedTask; |
| | | } |
| | | }, |
| | | OnMessageReceived = context => |
| | | { |
| | | if (context.HttpContext.IsSuccessSwagger() == true) |
| | | { |
| | | string token = context.HttpContext.GetSuccessSwaggerJwt(); |
| | | if (token.IsNotEmptyOrNull()) |
| | | { |
| | | context.Token = token; |
| | | } |
| | | } |
| | | return Task.CompletedTask; |
| | | }, |
| | | }; |
| | | }); |
| | | |