0

最近我升级了我的项目from .NET Framework 3.5 to 4.0.对于 WCF,我正在使用自定义基本身份验证。

在转换之前,一切正常。但是,转换后我得到“ Unexpected end of file error”。堆栈跟踪如下:

at System.Xml.EncodingStreamWrapper.ProcessBuffer(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding)
   at System.Xml.XmlUTF8TextReader.SetInput(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
   at System.Xml.XmlDictionaryReader.CreateTextReader(Byte[] buffer, Int32 offset, Int32 count, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
   at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at System.ServiceModel.Channels.WebMessageEncoderFactory.WebMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message)
   at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
   at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReplyCore(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReply(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout)
   at System.ServiceModel.Activation.HostedHttpContext.OnReply(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestContextBase.Reply(Message message)
   at ihv1Role.auth.BasicAuthenticationInterceptor.ProcessRequest(RequestContext& requestContext) in d:\src\auth\BasicAuthenticationInterceptor.cs:line 78

我收到错误的类和方法如下:

p

ublic class BasicAuthenticationInterceptor : RequestInterceptor
{
    MembershipProvider provider;
    string realm;

    public BasicAuthenticationInterceptor(MembershipProvider provider, string realm)
        : base(false)
    {
        this.provider = provider;
        this.realm = realm;
    }
    protected string Realm
    {
        get { return realm; }
    }
    protected MembershipProvider Provider
    {
        get { return provider; }
    }

    public override void ProcessRequest(ref RequestContext requestContext)
    {
        HttpRequestMessageProperty request = (HttpRequestMessageProperty)requestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name];
        string[] credentials = ExtractCredentials(requestContext.RequestMessage);

        if (credentials.Length > 0 && AuthenticateUser(credentials[0], credentials[1], IsTestMode))
        {
            InitializeSecurityContext(requestContext.RequestMessage, credentials[0]);
        }
        else
        {
            Message reply = Message.CreateMessage(MessageVersion.None, null);
            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty() { StatusCode = HttpStatusCode.Unauthorized };
            responseProperty.Headers.Add("WWW-Authenticate",
            String.Format("Basic realm=\"{0}\"", Realm));
            reply.Properties[HttpResponseMessageProperty.Name] = responseProperty;
            requestContext.Reply(reply); // **Here I am getting error**
            requestContext = null;
        }
    }
}

如果标头中的用户名和密码有效,则整个服务运行良好。仅当请求未经过身份验证时,我才会收到此错误。

有什么解决办法吗?

谢谢

4

1 回答 1

0

我自己解决了:

我在 Web.config 中添加了两次标签。

删除它,现在我的服务工作正常。

于 2013-09-27T09:22:59.060 回答