ajax - MVC ASP.net 核心 API 通过 Swagger 和外部 Web 应用程序获取域用户
问题描述
我有一个 ASP.net Core 3.1 MVC API。
我已经在上面设置了招摇,我的方法之一是 Authenticate() 它目前基于 windowsAuthentication 获取域用户。使用 HttpContext.User.Identity.Name。
问题
当 swagger 在 API 范围内调用该方法时,我得到了预期的用户名。
当应用程序通过 JQuery AJAX 调用 API 方法并允许凭据时,我从 IIS 获取应用程序池用户。
因此 API 可以从上下文中获取用户,除非从 Ajax 调用。
我可以通过获取用户并将其作为参数传递给 API 方法来解决这个问题,但这非常不安全。
调用 AJAX 时如何获取获取 windows 用户的 API?下面的代码是我的通用 ajax 方法。
call(properties, obj = null) {
let id = Helper.sessionNextID();
let headerText = {};
if (User) {
let token = User.getToken();
if (token.length > 0) {
headerText = {
"Authorization": `Bearer ${token}`
}
}
if (properties.spinner) {
Helper.spinner(true);
}
try {
$.ajax({
type: properties.type ?? "POST",
crossDomain: true,
async: true,
url: this.url + properties.url.trim(),
data: properties.data != null ? properties.data : {},
contentType: properties.contentType ?? "application/json; charset=utf-8",
processData: properties.processData ?? true,
headers: headerText,
xhrFields:
{
withCredentials: true,
crossDomain: true
},
beforeSend: function () {
Notification.notify(`Calling ${properties.url}`);
if (properties.beforeSend) {
properties.beforeSend()
}
Stats.start(id, properties.url)
$("navbar-icon").addClass('csa-icon-rotate').removeClass('csa-icon');
},
success: function (response) {
if (obj && obj.callingObj) {
obj.callingObj[properties.callBack](JSON.stringify(response), obj.data);
} else {
eval(properties.callBack + `(${JSON.stringify(response)})`);
}
Notification.notify(`successful ${properties.url}`);
},
failure: function (response) {
Stats.fail(id)
if (properties.failure) {
if (obj && obj.callingObj) {
obj.callingObj[properties.failure](`${JSON.stringify(response)})`)
} else {
eval(properties.failure + `(${JSON.stringify(response)})`);
}
}
Notification.notify(`Failed ${properties.url}`, "warn");
},
error: function (response) {
if (response.status == 0) {
Notification.notify(`<h2>Major Error</h2>The API is not responding. <br />${this.url}<br /> ${AppSettings.username()}`, "error")
} else {
Stats.error(id)
if (properties.error) {
if (obj && obj.callingObj) {
obj.callingObj[properties.error](`${JSON.stringify(response)})`)
} else {
eval(properties.error + `(${JSON.stringify(response)})`);
}
}
Notification.notify(`Returned with an error ${properties.url} ${response.responseText}`, "warn");
}
},
complete: function () {
Stats.end(id)
$("navbar-icon").addClass('csa-icon').removeClass('csa-icon-rotate');
Helper.spinner(false);
}
});
} catch (e) {
alert(e.message);
}
}
解决方案
推荐阅读
- dynamics-crm - Microsoft Dynamics API - Incident returned fields
- android - Cannot ping an added host into /etc/hosts - Android Issue
- ios - Swift:使用 AVAudioEngine (AVAudioPlayerNode) 播放无缝音频?
- sql - 在日期范围内计数不同,运行 Grass_date
- mysql - Access denied for user 'someuser'@'localhost' (using password: YES) when accessing via MySQL JDBC
- python - findPose() missing 1 required positional argument: 'self'
- c# - Topshelf remove the existing version of the service before install
- facebook-graph-api - Can not get facebook page token
- next.js - NextJs 中的重定向组件
- javascript - NodeJS 计时