You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.9 KiB

3 years ago
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Abstractions;
  3. using Microsoft.AspNetCore.Mvc.Authorization;
  4. using Microsoft.AspNetCore.Mvc.Filters;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging.Abstractions;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System.Text;
  10. using System;
  11. using System.Threading.Tasks;
  12. using Volo.Abp.AspNetCore.ExceptionHandling;
  13. using Volo.Abp.AspNetCore.Mvc;
  14. using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
  15. using Volo.Abp.Authorization;
  16. using Volo.Abp.DependencyInjection;
  17. using Volo.Abp.ExceptionHandling;
  18. using Volo.Abp.Http;
  19. using Volo.Abp.Json;
  20. using Microsoft.AspNetCore.Mvc;
  21. using Microsoft.AspNetCore.Authorization;
  22. using Microsoft.AspNetCore.Authorization.Policy;
  23. using System.Reflection.Metadata;
  24. using Microsoft.AspNetCore.Authentication;
  25. using System.Linq;
  26. using Microsoft.AspNetCore.Mvc.Controllers;
  27. using System.Net;
  28. namespace Shentun.Peis
  29. {
  30. // public class AuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler, ITransientDependency
  31. public class AuthorizationMiddlewareResultHandler
  32. {
  33. //public async Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
  34. //{
  35. // var endPoint = context.GetEndpoint();
  36. // var controllerActionDescriptor = (ControllerActionDescriptor)endPoint.Metadata
  37. // .ToList().FirstOrDefault(d => d is ControllerActionDescriptor);
  38. // var controllerName = controllerActionDescriptor.ControllerName;
  39. // var actionName = controllerActionDescriptor.ActionName;
  40. // if (!context.User.Identity.IsAuthenticated)
  41. // {
  42. // context.Response.StatusCode = (int)HttpStatusCode.OK;
  43. // // await context.Response.WriteAsync("{\"data\":{\"succeeded\":false,\"code\":401,\"message\":\"登录已过期,请重新登录\"}}");
  44. // return;
  45. // }
  46. // //else if (!await HandleRequirementEvaluateAsync(context.User, controllerName, actionName))
  47. // //{
  48. // // context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
  49. // // await context.Response.WriteAsync("{\"data\":{\"succeeded\":false,\"code\":403,\"message\":\"您暂⽆⾜够的权限执⾏该操作\"}}");
  50. // // return;
  51. // //}
  52. // await next(context);
  53. //}
  54. //public Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
  55. //{
  56. // if (authorizeResult.Succeeded)
  57. // {
  58. // return next(context);
  59. // }
  60. // return Handle();
  61. // async Task Handle()
  62. // {
  63. // if (authorizeResult.Challenged)
  64. // {
  65. // if (policy.AuthenticationSchemes.Count > 0)
  66. // {
  67. // foreach (var scheme in policy.AuthenticationSchemes)
  68. // {
  69. // await context.ChallengeAsync(scheme);
  70. // }
  71. // }
  72. // else
  73. // {
  74. // await context.ChallengeAsync();
  75. // }
  76. // }
  77. // else if (authorizeResult.Forbidden)
  78. // {
  79. // if (policy.AuthenticationSchemes.Count > 0)
  80. // {
  81. // foreach (var scheme in policy.AuthenticationSchemes)
  82. // {
  83. // await context.ForbidAsync(scheme);
  84. // }
  85. // }
  86. // else
  87. // {
  88. // await context.ForbidAsync();
  89. // }
  90. // }
  91. // }
  92. //}
  93. }
  94. }