4

我正在尝试设置将在我们公司内部网上运行的 WebAPI Web 服务和网站。我正在使用 IIS 来托管 web 服务 (A) 以及我的网站 (B)。我网站上的一个页面因此向网络服务发出请求:

var URL = 'http://MachineName:80/AWebService/api/GetGuid';
var request = new XMLHttpRequest();
request.open("GET", URL, false);
request.withCredentials = "true";
request.send();
var response = request.responseText;
return response;

WebService 代码如下所示:

[EnableCors(origins: "http://localhost", headers: "*", methods: "*")]
public class StoneSoupController : ApiController
{
    [ActionName("GetGuid")]
    public HttpResponseMessage GetGuid()
    {
        var indenty = this.User.Identity;
        Guid g = Guid.NewGuid();

        HttpResponseMessage msg = new HttpResponseMessage();
        msg.Content = new StringContent(g.ToString());
        msg.Headers.Add("Access-Control-Allow-Origin", "http://localhost"); //tried with and without this
        msg.Headers.Add("Access-Control-Allow-Credentials", "true"); //tried with and without this
        return msg;

    }
}

如果我在 IIS 中将 Web 服务的身份验证模式设置为匿名身份验证,则 Web 服务会按预期返回 guid 字符串。但是,我需要控制哪些用户能够访问 Web 服务上的某些方法并希望使用他们的 Windows 凭据来执行此操作。

我的问题是我似乎无法让 Firefox 发送 Windows 凭据。我已经尝试在 Firefox 中包含in http://localhost,但这似乎没有任何效果。network.automatic-ntlm-auth.trusted-urisabout-config

我已启用登录 IIS,这是它为请求记录的内容

2013-08-01 21:36:05 136.203.40.232 GET /AWebService/api/GetGuid - 80 - 136.203.40.232 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+rv:17.0)+Gecko/20100101+Firefox/17.0 200 0 0 965

如您所见,事务中没有用户 ID。

谁能帮我这个?

4

2 回答 2

1

当响应标头有时,浏览器将询问或自动发送用户凭据

WWW-认证 NTLM

第2步:

您需要将 Web api 的身份验证更改为 windows 并将授权属性添加到操作或控制器

第 3 步:Firefox 不会像 IE 那样发送凭据。您需要更改 Firefox 设置 about:config 将您的应用程序 url 设置为此变量 network.automatic-ntlm-auth.trusted-uris

于 2013-09-16T19:49:13.890 回答
0

关于您对 Giridhar 的评论:
可能是 IIS 没有启用“HTTP keep-alive”。

启用 HTTP Keep-Alives

  1. 在 IIS 管理器中,展开本地计算机,展开网站文件夹,右键单击网站,然后单击属性。

  2. 在“网站”选项卡的“连接”部分中,单击“启用 HTTP Keep-Alives”复选框。

  3. 单击应用,然后单击确定。

(来自微软

于 2013-12-04T13:07:25.257 回答