我有一个自定义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>
`
答案 0 :(得分:1)
解决方案是为我尝试修改的列表/库设置SharePoint权限。我太专注于服务的一端,我忘了考虑另一端。
我将<Authentication mode = "Windows"/>
添加到了web.config中。我不确定这实际上是否做了什么,但它并没有破坏服务。