小编Afs*_*hin的帖子

如何使用$ .Ajax将Windows身份验证凭据传递给Web API服务

道歉,如果问题是愚蠢的,说实话我自己觉得我错过了一些非常简单的东西......!

在这种情况下:我们有一个网站(A),它使用Windows身份验证,应用程序调用托管在同一服务器上的另一个web api站点(B),该服务器也使用Windows身份验证.

使用以下代码,我可以调用web apis并模拟用户:

var wi = (System.Security.Principal.WindowsIdentity)System.Web.HttpContext.Current.User.Identity;
wi.Impersonate();
using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
    //call the api ... successfully impersonated!
}
Run Code Online (Sandbox Code Playgroud)

以上我可以从站点A(在服务器端代码中)调用站点B上的web apis.

我的问题是在客户端(javascript),我需要调用ajax方法到GET/POST/... web apis.我知道我需要将xhrFields添加到ajax调用:

$.ajax({
    url: apiUrl,
    type: 'GET',
    xhrFields: {
        withCredentials: true
    },
    success: function (res) {
    },
    error: function (res) {
    }
});
Run Code Online (Sandbox Code Playgroud)

然而,这会提示输入凭据,而用户已经提供了浏览网站A的凭据.任何方式以某种方式将会话中的凭据(或令牌)包含到ajax调用中?

提前致谢,

javascript authentication ajax asp.net-web-api2

5
推荐指数
1
解决办法
812
查看次数