Web服务在一台服务器上失败(401 Unauthorized)但在另一台服务器上失败

时间:2009-12-28 14:44:48

标签: asp.net web-services unauthorized

我有两个相同的服务器。两者都是Win2k3。我有一个Web服务,它将电子邮件和使用该服务的应用程序排队。两者都使用相同的文件夹结构,权限和IIS设置托管在两台计算机上。一个称为“测试”,另一个称为“prod”。

理想情况下,prod上的应用程序将指向生产中的ws。但是,在这种情况下,我会收到以下错误页面:

  

'/ Apps / UTMv1'中的服务器错误   应用

     

请求因HTTP状态而失败   401:未经授权。描述:一个   未处理的异常发生在   当前网络的执行   请求。请查看堆栈跟踪   有关错误的更多信息   它起源于代码。

     

异常详细信息:   System.Net.WebException:请求   HTTP状态401失败:   未经授权的

     

来源错误:

     

生成了未处理的异常   在执行当前   网络请求。有关的信息   异常的起源和位置   可以使用例外来识别   堆栈跟踪下面。

     

堆栈追踪:

     

[WebException:请求失败了   HTTP状态401:未经授权。]
  System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage   消息,WebResponse响应,流   responseStream,Boolean asyncCall)   +431201 System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String   methodName,Object []参数)+204   utm.AppManagerEmailer.Emailer.AddToQueue(字符串   txtFrom,String txtTo,String   txtSubject,String txtBody,Int32   intAppId,Boolean blnIsBodyHtml)in   C:\ Documents and   Settings \用户名\桌面\项目\ UTM \ UTM \ WEB   参考\ AppManagerEmailer \ Reference.cs:93   utm.test.Page_Load(对象发送者,   EventArgs e)在C:\ Documents和   Settings \用户名\桌面\项目\ UTM \ UTM \ test.aspx.cs:23   错误帮助(IntPtr的   fp,Object o,Object t,EventArgs e)   +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object   发件人,EventArgs e)+35
  System.Web.UI.Control.OnLoad(EventArgs的   e)+99
  System.Web.UI.Control.LoadRecursive()   +50 System.Web.UI.Page.ProcessRequestMain(布尔值   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)+627

     

版本信息:Microsoft .NET   框架版本:2.0.50727.3053;   ASP.NET版本:2.0.50727.3053

如果prod上的应用程序指向测试中的ws,则可以正常工作。此外,如果测试中的应用程序指向生产中的ws,则可行。

为了好玩,我尝试在另一款应用中使用ws on prod。它的行为与原始应用程序相同。

另外,我在prod上运行其他正常运行的Web服务。这是唯一一个似乎造成问题的因素。

以下是我在应用中用来新建电子邮件并为电子邮件排队的代码:

Emailer e = new Emailer();
e.Credentials = System.Net.CredentialCache.DefaultCredentials;
int intEmailId = e.AddToQueue(fromEmail, toEmail, subject, body, 103, true);

以下是我的网络服务的代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Net.Mail;
using System.Collections.Generic;

namespace AppManager.WebServices
{
    /// <summary>
    /// Summary description for emailer
    /// </summary>
    [WebService(Namespace = "http://www.mydomain.com/apps/appManager/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Emailer : System.Web.Services.WebService
    {
        [WebMethod]
        public int AddToQueue(string txtFrom, string txtTo, string txtSubject, string txtBody, int intAppId, bool blnIsBodyHtml)
        {
            Email e = new Email()
            {
                From = new MailAddress(txtFrom),
                Subject = txtSubject,
                Body = txtBody,
                IsBodyHtml = blnIsBodyHtml,
                AppId = intAppId
            };
            e.To.Add(txtTo);
            e.AddToQueue();

            return e.Id;
        }

    }
}

那么,您认为可能是什么问题?

2 个答案:

答案 0 :(得分:1)

这听起来像是两台服务器之间的配置差异。

也就是说,您可以查看此KB http://support.microsoft.com/kb/896861

Microsoft添加了一些环回保护代码,如果FQDN不完全匹配,可能会导致401.1错误。

检查系统事件查看器以查看是否有更多详细信息。

答案 1 :(得分:0)

我相信您需要使用System.Net.CredentialCache.DefaultNetworkCredentials。至于它为何在测试中工作,您是否检查过该测试是否需要Windows集成身份验证并且不允许匿名访问?