C#PostAsync-{StatusCode:401,ReasonPhrase:“未经授权",使用ASP.NET MVC 4 [英] C# PostAsync - {StatusCode: 401, ReasonPhrase: 'Unauthorized' with ASP.NET MVC 4

查看:496
本文介绍了C#PostAsync-{StatusCode:401,ReasonPhrase:“未经授权",使用ASP.NET MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC4,并使用PostAsync从一个MVC4项目到另一个MVC4项目中调用控制器中的方法.我在调用的控制器或方法中未使用任何身份验证属性.

I am using ASP.NET MVC4 and calling a method within a controller from one MVC4 project to another, using PostAsync. I am not using any authentication attribute within the called controller or method.

引用此链接:链接 修复了在本地运行应用程序并向服务器上的应用程序调用PostAsync时的问题.现在,当我将应用程序部署到服务器并尝试调用其他应用程序时,我得到了未授权的错误.我在做什么错了?

Referencing this link: link Fixed my issue when running application locally and calling PostAsync to application on server. Now, when I deploy the application to the server and try to call the other application I get the unauthorized error. What am I doing wrong?

在IIS中运行的两个应用程序都使用相同的站点和相同的应用程序池.

Both application running in IIS are using the same site, and the same application pool.

使用远程调试,这是服务器响应:

Using remote debugging, this is the server response:

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Cache-Control: private
  Server: Microsoft-IIS/7.0
  WWW-Authenticate: Negotiate
  WWW-Authenticate: NTLM
  X-Powered-By: ASP.NET
  Date: Fri, 23 Jun 2017 13:44:19 GMT
  Content-Length: 6644
  Content-Type: text/html; charset=utf-8
}}

这是我的代码:

        using (var handler = new HttpClientHandler { UseDefaultCredentials = true })
        using (HttpClient client = new HttpClient(handler) { BaseAddress = new Uri(Properties.Settings.Default.EnvironmentRootPath)})
        {
            List<string> orderNumbers = NumList;
            var pairs = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("orderNumbers", string.Join(",", orderNumbers))
            };

            var content = new FormUrlEncodedContent(pairs);

            // call sync
            var response = client.PostAsync("CentralBroadcast/broadcastNewOrder", content).Result;
            if (response.IsSuccessStatusCode)
            {
                //
            }
            else
            {
                //
            }
        }

推荐答案

问题似乎是由于从承载IIS 5.1或更高版本并使用完全限定域名的同一服务器访问本地Web应用程序引起的.

The problem seemed to be due to accessing a local web application from the same server that hosts IIS 5.1 or greater and uses a fully qualified domain name.

FQDN的域名需要在注册表中列为白名单,而我可以通过完成以下步骤来做到这一点:

The domain names of the FQDN need to be white listed in the registry, and I was able to do this by completing the steps below:

在进行任何调整之前创建注册表备份总是一个好主意

1)单击开始,单击运行,键入regedit,然后单击确定.

1) Click Start, click Run, type regedit, and then click OK.

2)在注册表编辑器中,找到并单击下面的注册表项: HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Lsa \ MSV1_0

2) In Registry Editor, locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0

3)右键单击 MSV1_0 ,指向 New ,然后单击 Multi-String Value .

3) Right-click MSV1_0, point to New, and then click Multi-String Value.

4)键入BackConnectionHostNames,然后按Enter.

4) Type BackConnectionHostNames, and then press ENTER.

5)右键单击 BackConnectionHostNames ,然后单击修改".

5) Right-click BackConnectionHostNames, and then click Modify.

6)在值数据框中,键入主机名或本地计算机上站点的主机名,然后单击确定.

6) In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.

7)退出注册表编辑器,然后重新启动IISAdmin服务.

7) Quit Registry Editor, and then restart the IISAdmin service.

有关更多详细信息,请参考: https://support.microsoft.com/zh-cn/help/896861/you-receive-error-401.1-when-you-browse-a-web-site-that-uses-integrate

For more detailed information reference: https://support.microsoft.com/en-us/help/896861/you-receive-error-401.1-when-you-browse-a-web-site-that-uses-integrate

这篇关于C#PostAsync-{StatusCode:401,ReasonPhrase:“未经授权",使用ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆