forked from fanliang11/surging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomExceptionFilterAttribute.cs
76 lines (69 loc) · 2.44 KB
/
CustomExceptionFilterAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Surging.Core.ApiGateWay;
using Surging.Core.ApiGateWay.OAuth;
using Surging.Core.CPlatform.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GateWay.WebApi
{
public class ResponeMsg
{
public string IsSucceed { get; set; }
public int StatusCode { get; set; }
public string Message { get; set; }
}
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IModelMetadataProvider _modelMetadataProvider;
public CustomExceptionFilterAttribute(
IHostingEnvironment hostingEnvironment,
IModelMetadataProvider modelMetadataProvider)
{
_hostingEnvironment = hostingEnvironment;
_modelMetadataProvider = modelMetadataProvider;
}
public override void OnException(ExceptionContext context)
{
if (!_hostingEnvironment.IsDevelopment())
{
return;
}
var result = ServiceResult<object>.Create(false, errorMessage: "request fail");
result.StatusCode = 400;
context.Result = new JsonResult(result);
}
}
public class CustomAuthorizeFilter : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
var isSuccess = false;
var _authorizationServerProvider = ServiceLocator.GetService<IAuthorizationServerProvider>();
var author = context.HttpContext.Request.Headers["Authorization"];
if(author.Count > 0)
{
isSuccess = _authorizationServerProvider.ValidateClientAuthentication(author).Result;
}
if (true||!isSuccess)
{
context.Result = new UnauthorizedResult();
}
/* var entry =
Dns.GetHostEntryAsync(context.HttpContext.Connection.RemoteIpAddress)
.GetAwaiter()
.GetResult();
if (!entry.HostName.EndsWith(".MyDomain",
StringComparison.OrdinalIgnoreCase))
{
context.Result = new UnauthorizedResult();
}
*/
}
}
}