我有一个自定义SharePoint Web服务来执行某些操作。用户凭据通过Soap Header传递。凭证可以毫无问题地到达Web服务。
问题: 如果我没有使用用户凭据进行webservice调用,则以下代码返回“Unauthorized Error”。
WebService方法
[WebMethod(Description = "Test Credentials")]
[SoapDocumentMethod(Binding = "SPService")]
[SoapHeader("Authen")]
public string[] TestCredentials(string siteURL)
{
string[] credentials = new string[3];
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb spW = site.OpenWeb())
{
credentials[0] = Authen.CallingUserName;
credentials[1] = Authen.CallingUserPwd;
credentials[2] = siteURL;
}
}
return credentials;
}
客户代码
SPService.SPService spS = new SPService.SPService();//Webservice class
SPService.UserAuth ua = new SPService.UserAuth(); //Soap Header class
ua.CallingUserName = UserName; // User name
ua.CallingUserPwd = Password; // Password for the Username
spS.UserAuthValue = ua; // assigning the credential to the SoapHeader.
//System.Net.NetworkCredential nc = new NetworkCredential(UName, pwd, Domain);
//spS.Credentials = new NetworkCredential(UName, pwd, Domain);
string[] str = new string[3];
str = spS.TestCredentials(txt_URL.Text,"Shared Documents");
当网络凭据被注释掉时,我收到The request failed with HTTP status 401: Unauthorized.
错误。如果使用凭证,则webservice返回预期值。
Custom WebService的要求是使用WS-Security传递登录凭据并使用它来登录SharePoint站点。
一些帮助会很有用。如果您需要更多详细信息,请告诉我。