具有基本身份验证的WCF(远程服务器返回错误:404未找到)

时间:2018-04-12 13:39:53

标签: c# wcf authentication basic-authentication

我尝试在使用基本身份验证模块设置后,在外部(部署在IIS上)访问我的wcf(使用端口26880的http协议)服务,但它会引发以下错误:

  

远程服务器返回错误:(404)Not Found。

到目前为止我尝试过:

  1. 删除了基本身份验证,并检查了该服务是否可以在内部和外部访问,并且有效。

  2. 检查服务是否实际收到标头授权,并且用户已登录。(这有效)

  3. 在我的web.config文件中添加了以下行:
    <modules runAllManagedModulesForAllRequests="true"> 添加我的BasicAuthenticationModule之后仍然没有运气。

  4. 进入IIS并确保启用了BasicAuthentication。

  5. 启用了跟踪日志记录,显然除了查看此地址之外我看不到任何错误:http://localhost/ResponseService 实际上它应该是http://localhost:26880/ResponseService

  6. 我已经用谷歌搜索并尝试了我发现的与我的错误相关的所有可能的解决方案,到目前为止没有结果。

    这是我的web.config文件:

    <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>
    

    我的BasicAuthenticationModule以及BasicAuthenticationProvider(来源Custom Basic Authentication):

    public class BasicAuthenticationModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    
        public void Init(HttpApplication context)
        {
            context.AuthenticateRequest
            += new EventHandler(context_AuthenticateRequest);
        }
    
        void context_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            if (!BasicAuthenticationProvider.Authenticate(application.Context))
            {
                application.Context.Response.Status = "401 Unauthorized";
                application.Context.Response.StatusCode = 401;
                application.Context.Response.AddHeader("WWW - Authenticate", "Basic");
                application.CompleteRequest();
            }
        }
    }
    
    
    public class BasicAuthenticationProvider
    {
        public static bool Authenticate(HttpContext context)
        {
            if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization"))
            {
                return false;
            }
    
            string authHeader = HttpContext.Current.Request.Headers["Authorization"];
    
            IPrincipal principal;
            if (TryGetPrincipal(authHeader, out principal))
            {
                HttpContext.Current.User = principal;
                return true;
            }
            return false;
        }
    
        private static bool TryGetPrincipal(string authHeader, out IPrincipal principal)
        {
            var creds = ParseAuthHeader(authHeader);
    
            if (creds != null && TryGetPrincipal(creds, out principal))
            {
                return true;
            }
            else
            {
                principal = null;
                return false;
            }
        }
    
    
        private static string[] ParseAuthHeader(string authHeader)
        {
            if (
                authHeader == null ||
                authHeader.Length == 0 ||
                !authHeader.StartsWith("Basic")
            )
            {
                return null;
            }
    
            string base64Credentials = authHeader.Substring(6);
            string[] credentials = Encoding.ASCII.GetString(
                  Convert.FromBase64String(base64Credentials)
            ).Split(new char[] { ':' });
    
            if (credentials.Length != 2 ||
                string.IsNullOrEmpty(credentials[0]) ||
                string.IsNullOrEmpty(credentials[1])
            )
            {
                return null;
            }
    
            // Okay this is the credentials
            return credentials;
        }
    
        private static bool TryGetPrincipal(string[] creds, out IPrincipal principal)
        {
            if (ValidateLogin.IsLoginValid(new System.Net.NetworkCredential(creds[0], creds[1])))
            {
                principal = new GenericPrincipal(
                   new GenericIdentity(creds[0]),
                   new string[] { "Administrator", "Users" }
                );
                return true;
            }
            else
            {
                principal = null;
                return false;
            }
        }
    }
    

    这是我的客户:

    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设置不正确?有什么新想法吗?

0 个答案:

没有答案
相关问题