项目中有一个留言消息接口,接收其他系统的留言和展示留言,参考了网上的一些API验证方法,发现使用通用权限管理系统提供的验证方法最完美。
下面将实现的完整思路共享
1、WebApiConfig全局处理
?
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
|
/// <summary>
/// WebApiConfig
/// 路由基础配置。
///
///
/// 修改记录
///
/// 2016.11.01 版本:2.0 宋彪 对日期格式进行统一处理。
/// 2016.10.30 版本:2.0 宋彪 解决json序列化时的循环引用问题。
/// 2016.10.28 版本:2.0 宋彪 回传响应格式 $format 支持。
/// 2016.09.01 版本:1.0 宋彪 创建。
///
/// 版本:1.0
///
/// <author>
/// <name>宋彪</name>
/// <date>2016.09.01</date>
/// </author>
/// </summary>
public static class WebApiConfig
{
/// <summary>
/// 注册全局配置服务
/// </summary>
/// <param name="config"></param>
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
//强制https访问
//config.Filters.Add(new ForceHttpsAttribute());
// 统一回传格式
config.Filters.Add( new ApiResultAttribute());
// 发生异常时处理
config.Filters.Add( new ApiErrorHandleAttribute());
// ToKen身份验证过滤器 更方便 不需要在这里了 具有改标签的就会自动检查
//config.Filters.Add(new ApiAuthFilterAttribute());
// 解决json序列化时的循环引用问题
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
//对日期格式进行统一处理
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
new IsoDateTimeConverter()
{
DateTimeFormat = "yyyy-MM-dd hh:mm:ss"
}
);
// Web API routes 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi" ,
routeTemplate: "api/{controller}/{action}/{id}" ,
defaults: new { id = RouteParameter.Optional }
);
// 干掉XML序列化器
//config.Formatters.Remove(config.Formatters.XmlFormatter);
//在请求的Url加上 ?$format=xml,便可以指定响应格式
config.Formatters.XmlFormatter.AddQueryStringMapping( "$format" , "xml" , "application/xml" );
config.Formatters.JsonFormatter.AddQueryStringMapping( "$format" , "json" , "application/json" );
}
}
|
2、身份验证过滤器
?
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
|
using DotNet.Business;
using DotNet.Utilities;
using DotNet.Tracking.API.Common;
/// <summary>
/// ApiAuthFilterAttribute
/// 身份验证过滤器,具有ApiAuthFilterAttribute标签属性的方法会自动检查
///
///
/// 修改纪录
///
/// 2016-10-11 版本:1.0 SongBiao 创建文件。
///
/// <author>
/// <name>SongBiao</name>
/// <date>2016-10-11</date>
/// </author>
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true , AllowMultiple = true )]
public class ApiAuthFilterAttribute : AuthorizationFilterAttribute
{
/// <summary>
/// 未授权时的提示信息
/// </summary>
private const string UnauthorizedMessage = "请求未授权,拒绝访问。" ;
/// <summary>
/// 权限进入
/// </summary>
/// <param name="actionContext"></param>
public override void OnAuthorization(HttpActionContext actionContext)
{
base .OnAuthorization(actionContext);
// 允许匿名访问
if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Count > 0)
{
return ;
}
string systemCode = APIOperateContext.Current.SystemCode;
string permissionCode = APIOperateContext.Current.PermissionCode;
string appKey = APIOperateContext.Current.AppKey;
string appSecret = APIOperateContext.Current.AppSecret;
if ( string .IsNullOrWhiteSpace(appKey) || string .IsNullOrWhiteSpace(appSecret))
{
//未验证(登录)的用户, 而且是非匿名访问,则转向登录页面
//actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
//actionContext.Response.Content = new StringContent("<p>Unauthorized</p>", Encoding.UTF8, "text/html");
var response = actionContext.Response= actionContext.Response?? new HttpResponseMessage();
response.StatusCode = HttpStatusCode.Unauthorized;
BaseResult result = new BaseResult
{
Status = false ,
StatusMessage = UnauthorizedMessage
};
response.Content = new StringContent(result.ToJson(), Encoding.UTF8, "application/json" );
}
else
{
// 检查 AppKey 和 AppSecret
BaseResult result = BaseServicesLicenseManager.CheckService(appKey, appSecret, false , 0, 0, systemCode, permissionCode);
if (!result.Status)
{
var response = actionContext.Response = actionContext.Response?? new HttpResponseMessage();
response.Content = new StringContent(result.ToJson(), Encoding.UTF8, "application/json" );
}
}
}
}
|
3、统一回传格式
?
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
|
/// <summary>
/// ApiResultAttribute
/// 统一回传格式
///
/// 修改纪录
///
/// 2016-10-31 版本:1.0 宋彪 创建文件。
///
/// <author>
/// <name>宋彪</name>
/// <date>2016-10-31</date>
/// </author>
/// </summary>
public class ApiResultAttribute : ActionFilterAttribute
{
/// <summary>
/// 重写回传的处理
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
// 快件跟踪接口传的是format,不用走这里
if (actionExecutedContext.Request.Properties.ContainsKey( "format" ))
{
// 若发生例外则不在这边处理 在异常中处理 ApiErrorHandleAttribute
if (actionExecutedContext.Exception != null )
return ;
base .OnActionExecuted(actionExecutedContext);
var result = new ApiResultModel();
// 取得由 API 返回的状态码
result.Status = actionExecutedContext.ActionContext.Response.StatusCode;
// 取得由 API 返回的资料
result.Data = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync< object >().Result;
// 重新封装回传格式
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.Status, result);
}
}
}
|
4、全局异常处理
?
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
|
using DotNet.Utilities;
using DotNet.Tracking.API.Common;
using DotNet.Tracking.API.Controllers;
using DotNet.Tracking.API.Models;
/// <summary>
/// ApiErrorHandleAttribute
/// 全局异常处理
///
/// 修改纪录
///
/// 2016-10-31 版本:1.0 宋彪 创建文件。
///
/// <author>
/// <name>宋彪</name>
/// <date>2016-10-31</date>
/// </author>
/// </summary>
public class ApiErrorHandleAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
{
/// <summary>
/// 异常统一处理
/// </summary>
/// <param name="actionExecutedContext"></param>
public override void OnException(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
{
base .OnException(actionExecutedContext);
// 取得发生例外时的错误讯息
var errorMessage = actionExecutedContext.Exception.Message;
// 异常记录
string parameters = APIOperateContext.GetRequestParameters();
NLogHelper.Trace(actionExecutedContext.Exception, BaseSystemInfo.SystemCode + " ApiErrorHandleAttribute OnException 完整的请求地址及参数 : " + parameters);
// 2016-11-01 加入异常邮件提醒
NLogHelper.InfoMail(actionExecutedContext.Exception, BaseSystemInfo.SystemCode + " ApiErrorHandleAttribute OnException 完整的请求地址及参数 : " + parameters);
var result = new ApiResultModel()
{
Status = HttpStatusCode.BadRequest,
ErrorMessage = errorMessage
};
// 重新打包回传的讯息
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(result.Status, result);
}
}
|
5、接口操作的上下文
?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|