BizTalk WCF 服务中的 WebRequest 凭据

WebRequest Credentials in BizTalk WCF Service

我有一个自定义 WCF 服务,它使用 WebRequest 更新我的 SharePoint 网站上的文件。但是在将服务部署到 BizTalk 后尝试使用该服务时出现 401 Unauthorized 错误。

我已经尝试为 WebRequest 对象设置凭据,如果我对实际帐户进行硬编码,它就会起作用。但这不是我可以留下的东西。

有没有人必须开发使用 WebRequest 修改 SharePoint 的自定义 BizTalk 服务?您是如何处理凭据的?

错误位置

 public static void uploadSharePointFile(Uri fileUri, byte[] myByteArray, ref List<string> result)
 {
        Stream dataStream = null;
        WebResponse response = null;
        try
        {
            result.Add("Requesting PUT of SharePoint File\n  " + fileUri.ToString());
            WebRequest request = WebRequest.Create(fileUri);
            request.UseDefaultCredentials = true;
            request.PreAuthenticate = true;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            request.Method = "PUT";
            request.ContentType = "text/xml";
            request.ContentLength = myByteArray.Length;
            result.Add("File Size: " + request.ContentLength + " Bytes");
            dataStream = request.GetRequestStream();
            dataStream.Write(myByteArray, 0, myByteArray.Length);
            dataStream.Close();
            result.Add("Data stored to SharePoint File");

            response = request.GetResponse();
            result.Add("Response Status Description: " + ((HttpWebResponse)response).StatusDescription);
            dataStream = response.GetResponseStream();
        }
        catch (WebException e)
        {
            result.Add("(401) Unauthorized");
            result.Add(e.InnerException.Message);
        }
        finally
        {
            if (dataStream != null) dataStream.Close();
            if (response != null) response.Close();
        }
}

Web.Config

enter code here
<configuration>

  <configSections>
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferSize="65536000" maxReceivedMessageSize="65536000" maxBufferPoolSize="65536000">
          <readerQuotas maxBytesPerRead="65536000" maxArrayLength="65536000" maxDepth="65536000"/></binding>
      </basicHttpBinding>
      <basicHttpsBinding>
        <binding maxBufferSize="65536000" maxReceivedMessageSize="65536000" maxBufferPoolSize="65536000">
          <readerQuotas maxBytesPerRead="65536000" maxArrayLength="65536000" maxDepth="65536000"/></binding>
      </basicHttpsBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <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"/>
  </system.webServer>

</configuration>

`

解决方案是为我尝试修改的 list/library 在 SharePoint 上设置权限。我太专注于服务的一端,以至于忘记了考虑另一端。

然后我将 <Authentication mode = "Windows"/> 添加到 web.config。我不确定这是否真的做了任何事情,但它并没有破坏服务。