Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自控制台应用程序身份验证标头的ASP.NET Web Api调用_C#_Authentication_Asp.net Web Api_Dotnet Httpclient - Fatal编程技术网

C# 来自控制台应用程序身份验证标头的ASP.NET Web Api调用

C# 来自控制台应用程序身份验证标头的ASP.NET Web Api调用,c#,authentication,asp.net-web-api,dotnet-httpclient,C#,Authentication,Asp.net Web Api,Dotnet Httpclient,我有两份申请。一个是ASP.NETWebAPI,另一个是控制台应用程序,我从中调用Api。调试时,我总是得到401.2响应。Fiddler给出了这个回答 由于身份验证标头无效,您无权查看此页 我的问题是如何配置IIS Express以及如何在控制台应用程序中设置头以正确调用Web Api?我想进行Windows或匿名身份验证。我还在IIS Express中启用了Windows身份验证 控制台应用程序代码: MachineStateModel model = new MachineStateMod

我有两份申请。一个是ASP.NETWebAPI,另一个是控制台应用程序,我从中调用Api。调试时,我总是得到401.2响应。Fiddler给出了这个回答

由于身份验证标头无效,您无权查看此页

我的问题是如何配置IIS Express以及如何在控制台应用程序中设置头以正确调用Web Api?我想进行Windows或匿名身份验证。我还在IIS Express中启用了Windows身份验证

控制台应用程序代码:

MachineStateModel model = new MachineStateModel();
model.DataType = "1";
model.MachineID = 1;
model.TimeStamp = DateTime.Now;
model.Value = "0";
HttpClientHandler handler = new HttpClientHandler() { UseDefaultCredentials = true };
using (var Client = new HttpClient(handler))
{
    Client.BaseAddress = new Uri("http://localhost.fiddler:55308/");
    Client.DefaultRequestHeaders.Accept.Clear();
    Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = await Client.PostAsJsonAsync("api/machinestate", model);
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine("Call is successful");
    }
}
  <system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<customErrors mode="Off" />
<authentication mode="Windows"/>
<authorization>
  <allow users="?"/>
</authorization>
    // POST api/values
    public HttpResponseException Post([FromBody]MachineStateModel value)
    {
        XmlMStateStream CurrentStream = new XmlMStateStream();
        CurrentStream.DateTime = value.TimeStamp;
        CurrentStream.MachineID = value.MachineID;
        CurrentStream.DataType = value.DataType;
        CurrentStream.Value = value.Value;

        HttpResponseMessage responsemessage = Request.CreateResponse(HttpStatusCode.OK, CurrentStream);
        HttpResponseException response = new HttpResponseException(responsemessage);
        return response;
    }
ASP.NET Web Api Web.config:

MachineStateModel model = new MachineStateModel();
model.DataType = "1";
model.MachineID = 1;
model.TimeStamp = DateTime.Now;
model.Value = "0";
HttpClientHandler handler = new HttpClientHandler() { UseDefaultCredentials = true };
using (var Client = new HttpClient(handler))
{
    Client.BaseAddress = new Uri("http://localhost.fiddler:55308/");
    Client.DefaultRequestHeaders.Accept.Clear();
    Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = await Client.PostAsJsonAsync("api/machinestate", model);
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine("Call is successful");
    }
}
  <system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<customErrors mode="Off" />
<authentication mode="Windows"/>
<authorization>
  <allow users="?"/>
</authorization>
    // POST api/values
    public HttpResponseException Post([FromBody]MachineStateModel value)
    {
        XmlMStateStream CurrentStream = new XmlMStateStream();
        CurrentStream.DateTime = value.TimeStamp;
        CurrentStream.MachineID = value.MachineID;
        CurrentStream.DataType = value.DataType;
        CurrentStream.Value = value.Value;

        HttpResponseMessage responsemessage = Request.CreateResponse(HttpStatusCode.OK, CurrentStream);
        HttpResponseException response = new HttpResponseException(responsemessage);
        return response;
    }

尝试在\My Documents\IISExpress\config\applicationhost.config中的IIS Express中启用Windows身份验证:

<system.webServer>
...
  <security>
...
    <authentication>
      <windowsAuthentication enabled="true" />
   </authentication>
...
  </security>
...
</system.webServer>

...
...
...
...

这成功实现了匿名身份验证(最高投票答案)。Windows身份验证呢?我必须为HttpClient请求提供一些头吗?您必须传递web api正在寻找的头。您的web api有任何授权过滤器吗?如果是,则您必须检查web api正在查找的详细信息。UseDefaultCredentials应足以从控制台应用程序执行Windows身份验证。我刚刚测试了它,它在HttpListener托管的服务器上运行。AuthenticationManager类负责使用适当的授权标头响应www authenticate标头。