ASP REST服务 - 身份验证问题

时间:2015-10-29 03:58:57

标签: c# asp.net .net web-services rest

我在我的问题上找到了大量的材料/信息,但我发现的任何东西似乎都无法修复它。

我编写了一个ASP REST服务,它使用NTLM进行身份验证,将Jpeg作为流接收(将图像保存到服务器),并且我无法使其工作。

namespace Services
{
    [System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
        InstanceContextMode = InstanceContextMode.PerCall,
        IgnoreExtensionDataObject = true,
        IncludeExceptionDetailInFaults = true,
        AddressFilterMode = AddressFilterMode.Any)]
    public class MyService : IMyService
    {
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public bool Upload(Stream image)
        {
            //code here
            return true;
        }
    }
}

web.config如下:

  <system.web>
    <compilation targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" executionTimeout="4800" maxRequestLength="500000000"/>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="defaultBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="Service.MyService" behaviorConfiguration="defaultBehavior">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="defaultWebHttpBinding" contract="MyService.IMyService" behaviorConfiguration="web" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="defaultWebHttpBinding"
                 closeTimeout="00:02:00"  openTimeout="00:02:00" receiveTimeout="00:02:00" sendTimeout="00:02:00" 
                 maxBufferPoolSize="250000000" maxReceivedMessageSize="250000000">
          <readerQuotas maxBytesPerRead="2147483647" maxDepth="4500000" maxStringContentLength="4500000"  maxNameTableCharCount="250000000" maxArrayLength="4500000"/>
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Ntlm" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="500000000"></requestLimits>
      </requestFiltering>
    </security>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, accept, authorization, origin, referer, user-agent, x-token"/>
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>

在IIS上,它仅使用Windows身份验证运行。从这段代码调用它:         var wcfServiceUrl =&#39; http://server/MyService.svc/Upload/&#39;;

    function uploadImage(image) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', wcfServiceUrl, true);
        xhr.send(photo);
    }

如果我从本地服务器(即从服务器上的html页面)调用它,这是有效的。但是,如果我从其他任何地方运行它(本地的test.html页面,另一台服务器上的托管网页),它将失败并显示以下内容:

OPTIONS http://server/MyService.svc/Upload/ 401 (Unauthorized)
XMLHttpRequest cannot load http://server/MyService.svc/Upload/. Response for preflight has invalid HTTP status code 401

标题如下:

Remote Address:0.0.0.0
Request URL:http://server/MyService.svc/Upload/
Request Method:OPTIONS
Status Code:401 Unauthorized

Response Headers
Access-Control-Allow-Headers:Content-Type, accept, authorization, origin, referer, user-agent, x-token
Access-Control-Allow-Methods:POST,GET,OPTIONS
Access-Control-Allow-Origin:*
Content-Length:1293
Content-Type:text/html
Date:Thu, 29 Oct 2015 03:46:52 GMT
Server:Microsoft-IIS/7.5
WWW-Authenticate:NTLM
X-Powered-By:ASP.NET

Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:server
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

0 个答案:

没有答案