Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
Education
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
Education
Commits
9e3dd12d
Commit
9e3dd12d
authored
Jan 21, 2021
by
liudong1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
过滤器增加 控制器方法重复提交验证
parent
d36429b6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
16 deletions
+36
-16
AllowRepeatAttribute.cs
Edu.WebApi/Filter/AllowRepeatAttribute.cs
+13
-0
ApiFilterAttribute.cs
Edu.WebApi/Filter/ApiFilterAttribute.cs
+23
-16
No files found.
Edu.WebApi/Filter/AllowRepeatAttribute.cs
0 → 100644
View file @
9e3dd12d
using
System
;
namespace
Edu.WebApi.Filter
{
/// <summary>
/// 允许重复提交表单属性
/// </summary>
public
class
AllowRepeatAttribute
:
Attribute
{
}
}
\ No newline at end of file
Edu.WebApi/Filter/ApiFilterAttribute.cs
View file @
9e3dd12d
...
...
@@ -12,6 +12,7 @@ using Edu.Common.Plugin;
using
Edu.Common.API
;
using
Edu.Cache.User
;
using
Edu.WebApi.Helper
;
using
System.Reflection
;
namespace
Edu.WebApi.Filter
{
...
...
@@ -43,31 +44,37 @@ namespace Edu.WebApi.Filter
}
#
region
验证表单重复提交
string
controllerName
=
actionContext
.
ActionDescriptor
.
RouteValues
[
"controller"
].
ToString
().
ToLower
();
string
actionName
=
actionContext
.
ActionDescriptor
.
RouteValues
[
"action"
].
ToString
().
ToLower
();
var
action
=
actionContext
.
ActionDescriptor
as
Microsoft
.
AspNetCore
.
Mvc
.
Controllers
.
ControllerActionDescriptor
;
string
controllerName
=
action
.
ControllerName
.
ToLower
();
string
actionName
=
action
.
ActionName
.
ToLower
();
if
(!
actionName
.
ToLower
().
Contains
(
"get"
))
{
string
cachedKey
=
SecurityHelper
.
MD5
(
string
.
Format
(
"cmd={0}&token={1}"
,
controllerName
+
"/"
+
actionName
,
token
));
try
var
allowRepeat
=
action
.
MethodInfo
.
GetCustomAttribute
(
typeof
(
AllowRepeatAttribute
));
if
(
allowRepeat
==
null
)
{
if
(
UserReidsCache
.
Exists
(
cachedKey
))
//判断表单是否重复提交
//需进行表单重复提交验证
string
cachedKey
=
SecurityHelper
.
MD5
(
string
.
Format
(
"cmd={0}&token={1}"
,
controllerName
+
"/"
+
actionName
,
token
));
try
{
actionContext
.
Result
=
new
Microsoft
.
AspNetCore
.
Mvc
.
JsonResult
(
new
ApiResult
if
(
UserReidsCache
.
Exists
(
cachedKey
))
//判断表单是否重复提交
{
Code
=
(
int
)
ResultCode
.
FormRepeatSubmit
,
Message
=
"表单重复提交,请稍后再试"
,
Data
=
null
});
actionContext
.
Result
=
new
Microsoft
.
AspNetCore
.
Mvc
.
JsonResult
(
new
ApiResult
{
Code
=
(
int
)
ResultCode
.
FormRepeatSubmit
,
Message
=
"表单重复提交,请稍后再试"
,
Data
=
null
});
}
else
{
//默认2秒钟之内不能重复提交
UserReidsCache
.
Set
(
cachedKey
,
1
,
2
);
}
}
else
catch
{
//默认2秒钟之内不能重复提交
UserReidsCache
.
Set
(
cachedKey
,
1
,
2
);
}
}
catch
{
}
}
#
endregion
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment