C# 具有基本身份验证的WCF(远程服务器返回错误:404未找到)
在使用基本身份验证模块设置wcf(使用端口26880的http协议)服务后,我尝试从外部(部署在IIS上)访问该服务,但它会引发以下错误: 远程服务器返回错误:(404)未找到 到目前为止,我所尝试的:C# 具有基本身份验证的WCF(远程服务器返回错误:404未找到),c#,wcf,authentication,basic-authentication,C#,Wcf,Authentication,Basic Authentication,在使用基本身份验证模块设置wcf(使用端口26880的http协议)服务后,我尝试从外部(部署在IIS上)访问该服务,但它会引发以下错误: 远程服务器返回错误:(404)未找到 到目前为止,我所尝试的: 删除了基本身份验证,并检查该服务是否可以在内部和外部访问,以及是否正常工作 检查服务是否实际收到了标头授权,并且用户已登录。(此操作有效) 在我的web.config文件中添加了以下行: 在添加我的基本身份验证模块后,仍然没有运气 进入IIS并确保基本身份验证已启用 启用了跟踪日志记录,显然除
在添加我的基本身份验证模块后,仍然没有运气http://localhost/ResponseService
实际上它应该是http://localhost:26880/ResponseService
<configuration>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime maxRequestLength="51200" enable="true" executionTimeout="60000"/>
<customErrors mode="Off"/>
<authentication mode="None"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="1" multipleSiteBindingsEnabled="true"/>
<services>
<service behaviorConfiguration="Default" name="dcs_response_handler.ResponseService">
<host>
<baseAddresses>
<add baseAddress="http://'external ip':26880/DcsResponse/"/>
</baseAddresses>
</host>
<endpoint name="webHttpBinding" contract="dcs_response_handler.IResponseService" binding="webHttpBinding" address="" bindingConfiguration="webHttpBinding" behaviorConfiguration="webBehavior"/>
<endpoint name="mexHttpBinding" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<protocolMapping>
<add binding="webHttpBinding" scheme="http"/>
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior name="Default">
<useRequestHeadersForMetadataAddress/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior>
<useRequestHeadersForMetadataAddress/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding"
transferMode="Streamed">
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
</security>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="BasicAuthenticationModule"/>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
<add name="BasicAuthenticationModule" type="dcs_response_handler.BasicAuthenticationModule" preCondition=""/>
</modules>
<handlers>
<remove name="svc-Integrated-4.0" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<security>
<authentication>
<windowsAuthentication enabled="false"/>
<anonymousAuthentication enabled="false"/>
</authentication>
<requestFiltering>
<requestLimits maxAllowedContentLength="32000000" />
</requestFiltering>
</security>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource"
switchValue="Information, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="Error.svclog" />
</sharedListeners>
</system.diagnostics>
</configuration>
这是我的客户:
public static void ServiceClientTest()
{
HttpWebRequest req = null;
HttpWebResponse resp = null;
string SampleXml = string.Empty;
string baseAddress = "http://'external ip':26880/DcsResponse/XMLGate/inbound?System=84771500";
try
{
string strResult = string.Empty;
using (StreamReader readFile = new StreamReader(@"C:\Users\Alin\Desktop\testingXml.xml"))
{
SampleXml = readFile.ReadToEnd();
}
string postData = SampleXml.ToString();
byte[] data = Encoding.UTF8.GetBytes(postData);
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("alin" + ":" + "Dcs1234"));
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(baseAddress);
webrequest.Headers.Add("Authorization", "Basic " + svcCredentials);
webrequest.Method = "POST";
webrequest.ContentType = "text/xml";
webrequest.ContentLength = data.Length;
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
if (webresponse.StatusCode == HttpStatusCode.OK)
{
Stream stream = webresponse.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
catch (WebException webEx)
{
Console.WriteLine("Web exception :" + webEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("Exception :" + ex.Message);
}
}
根据我得到的错误,我假设它找不到文件或者我的web.config设置不正确?有什么新想法吗?通过将绑定从BasicHttpBinding更改为WebHttpBinding,并将安全模式从Transport更改为None(更新了web.config),最终实现了该功能。我仍然不确定为什么会出现404 Not found错误,但我首先删除了BasicAuthorization模块,并重新测试了该服务,确保我可以从内部和外部访问它。之后,我实现了webHttpBinding,并将安全模式设置为None,这几乎使一切正常工作。现在我有了一个具有基本身份验证的完整功能的WCF服务。希望这能帮助那些和我一样的人。
public static void ServiceClientTest()
{
HttpWebRequest req = null;
HttpWebResponse resp = null;
string SampleXml = string.Empty;
string baseAddress = "http://'external ip':26880/DcsResponse/XMLGate/inbound?System=84771500";
try
{
string strResult = string.Empty;
using (StreamReader readFile = new StreamReader(@"C:\Users\Alin\Desktop\testingXml.xml"))
{
SampleXml = readFile.ReadToEnd();
}
string postData = SampleXml.ToString();
byte[] data = Encoding.UTF8.GetBytes(postData);
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("alin" + ":" + "Dcs1234"));
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(baseAddress);
webrequest.Headers.Add("Authorization", "Basic " + svcCredentials);
webrequest.Method = "POST";
webrequest.ContentType = "text/xml";
webrequest.ContentLength = data.Length;
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
if (webresponse.StatusCode == HttpStatusCode.OK)
{
Stream stream = webresponse.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}
catch (WebException webEx)
{
Console.WriteLine("Web exception :" + webEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("Exception :" + ex.Message);
}
}