WCF REST服务 - 执行时出现HTTP 404错误

时间:2017-12-11 07:52:01

标签: c# rest wcf iis basic-authentication

我在 IIS 中使用 WCF REST服务基本身份验证(用户名/密码)

我的界面 - ICustomer.cs 如下:

namespace SampleService
{
    [ServiceContract]
    public interface ICustomer
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
             RequestFormat = WebMessageFormat.Json,
             ResponseFormat = WebMessageFormat.Json,
             UriTemplate = "/Customer/{email}")]
        string GetCustomerID(string email);
    }
}

Customer.svc 如下:

<%@ ServiceHost Language="C#" Debug="true" Service="SampleService.Customer" CodeBehind="Customer.svc.cs" %>

Customer.svc.cs

namespace SampleService
{
    public class Customer : ICustomer
    {
        public string GetCustomerID(string strEmail)
        {
            string returnJsonString = bllContactDetails.GetCustomerID(strEmail).ToString(Formatting.None);
            WebOperationContext customContext;
            customContext = Utility.General.SetCustomHttpStatusCode(returnJsonString);
            return returnJsonString;
        }
    }    

    public class RestAuthorizationManager : ServiceAuthorizationManager
    {

        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            //Extract the Authorization header, and parse out the credentials converting the Base64 string:
            var authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];
            if ((authHeader != null) && (authHeader != string.Empty))
            {
                var svcCredentials = System.Text.ASCIIEncoding.ASCII
                        .GetString(Convert.FromBase64String(authHeader.Substring(6)))
                        .Split(':');
                var user = new { Name = svcCredentials[0], Password = svcCredentials[1] };
                if ((user.Name == "testuser" && user.Password == "testpassword"))
                {
                    //User is authrized and originating call will proceed
                    return true;
                }
                else
                {
                    //not authorized
                    return false;
                }
            }
            else
            {
                //No authorization header was provided, so challenge the client to provide before proceeding:
                WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm=\"MyWCFService\"");
                //Throw an exception with the associated HTTP status code equivalent to HTTP status 401
                throw new WebFaultException(HttpStatusCode.Unauthorized);
            }
        }
    }

}

我已添加 Web.Config ,如下所示:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" 
                           httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceAuthorization serviceAuthorizationManagerType=" SampleService.RestAuthorizationManager, SampleService"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpServiceBehavior">
          <!-- Important this is the behavior that makes a normal WCF service to REST based service-->
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="SampleService.Customer" behaviorConfiguration="ServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://192.168.0.122:443/" />
          </baseAddresses>
        </host>
        <endpoint binding="webHttpBinding" contract="SampleService.ICustomer" behaviorConfiguration="webHttpServiceBehavior" />
      </service>
    </services>
  </system.serviceModel>

我已在 IIS 中创建了自定义SSL 并启用了基本身份验证

在浏览器中查看我的IIS托管WCF服务时,它会显示所有服务列表。 List of services displayed

但是当我尝试使用 Customer.svc 方法获取数据时,它会抛出错误,如下所示:

  

HTTP 404.您正在寻找的资源(或其中一个   依赖项)可能已被删除,其名称已更改,或者是   暂时不可用。请查看以下网址并制作   确保它拼写正确。

请找error screenshot with URL for reference.

另外,如果我使用的是用于获取基本身份验证的正确绑定,请告诉我。

0 个答案:

没有答案