forked from VahidN/DNTCaptcha.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
195 lines (176 loc) · 6.8 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>DNTCaptcha.Core</title>
<meta charset="utf-8"/>
<link href="/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="/lib/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"/>
<link href="/content/site.css" rel="stylesheet"/>
<script src="/lib/jquery/dist/jquery.min.js" type="text/javascript"></script>
<script src="/lib/jquery-validation/dist/jquery.validate.min.js" type="text/javascript"></script>
<script src="/lib/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.min.js"
type="text/javascript"></script>
<script src="/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<h3>Login Form</h3>
<form action="" id="loginForm" method="post" novalidate>
<div class="mb-3">
<label class="form-label">User Name</label>
<input class="form-control" name="username" type="text"/>
</div>
<div class="mb-3">
<label>Password</label>
<input class="form-control" name="password" type="password"/>
</div>
<div class="dntCaptcha mb-3">
<img
alt="captcha"
id="dntCaptchaImg"
name="dntCaptchaImg"
style="margin-bottom: 4px"
/>
<a
class="bi-arrow-counterclockwise btn-lg"
id="dntCaptchaRefreshButton"
name="dntCaptchaRefreshButton"
onclick="doRefreshCaptcha()"
style="cursor: pointer"
></a>
<input id="DNTCaptchaText" name="DNTCaptchaText" type="hidden"/>
<div class="input-group">
<span class="input-group-text">
<span class="bi-lock"></span>
</span>
<input
autocomplete="off"
class="form-control"
dir="ltr"
id="DNTCaptchaInputText"
name="DNTCaptchaInputText"
placeholder="Security code as a number"
type="text"
/>
</div>
<input id="DNTCaptchaToken" name="DNTCaptchaToken" type="hidden"/>
</div>
<button class="btn btn-secondary" id="submitLogin" type="submit">
Submit
</button>
<div
class="alert alert-warning"
id="validationErrorMessage"
style="display: none"
></div>
</form>
</div>
<script type="text/javascript">
const captchaApiUrl =
"https://localhost:5001/api/account/CreateDNTCaptchaParams";
const loginUrl = "https://localhost:5001/api/account/login";
const loginFormId = "#loginForm";
const submitButtonId = "#submitLogin";
const validationErrorMessageId = "#validationErrorMessage";
function getCaptchaInfo() {
$.ajax({
url: captchaApiUrl,
type: "GET",
}).then(
function (response) {
console.log(response);
const {
dntCaptchaImgUrl,
dntCaptchaId,
dntCaptchaTextValue,
dntCaptchaTokenValue,
} = response;
$("#dntCaptchaImg").attr("src", dntCaptchaImgUrl);
$("#DNTCaptchaText").attr("value", dntCaptchaTextValue);
$("#DNTCaptchaToken").attr("value", dntCaptchaTokenValue);
$("div.dntCaptcha").attr("id", dntCaptchaId);
},
function (xhr, status, error) {
console.log({xhr: xhr, status: status, error: error});
if (xhr.status === 429) {
alert('Too many requests! Please wait a minute!');
}
}
);
}
function doRefreshCaptcha() {
$("#DNTCaptchaInputText").val("");
getCaptchaInfo();
}
function getFormDataAsJsonObject($form) {
var unIndexedArray = $form.serializeArray();
var indexedArray = {};
$.map(unIndexedArray, function (n, i) {
indexedArray[n["name"]] = n["value"];
});
return indexedArray;
}
function errorToString(obj) {
const parts = [];
parts.push("<ul>");
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
if (typeof value === "object") {
return errorToString(value);
}
if (value !== null && value !== undefined) {
parts.push(`<li>${value}</li>`);
}
}
}
parts.push("</ul>");
return parts.join("");
}
$(function () {
$.ajaxSetup({xhrFields: {withCredentials: true}}); // Set this if you are using `CORS`.
getCaptchaInfo();
// Post login form to the server
$(submitButtonId).on("click", function (event) {
event.preventDefault();
$(validationErrorMessageId).html("");
$(validationErrorMessageId).hide();
$.ajax({
url: loginUrl,
type: "POST",
data: $(loginFormId).serialize(), // serializes the form's elements as a form-urlencoded data.
/*
NOTE: Don't post it as a JSON data. Its `Content-Type` should be `application/x-www-form-urlencoded`.
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(getFormDataAsJsonObject($(loginFormId))),
*/
success: function (result) {
console.log(result);
alert("Succeed!");
doRefreshCaptcha();
},
error: function (xhr, status, error) {
console.log({xhr: xhr, status: status, error: error});
if (xhr.status === 429) {
alert('Too many requests! Please wait a minute!');
return;
}
if (xhr.status === 400) {
alert("Failed! Try again!");
$(validationErrorMessageId).html(
errorToString(xhr.responseJSON)
);
$(validationErrorMessageId).show();
} else {
alert(xhr.responseText);
}
doRefreshCaptcha();
},
});
});
});
</script>
</body>
</html>