webservice 认证(sharepoint 客服端对象模型 )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UserHeader : System.Web.Services.Protocols.SoapHeader
{
public UserHeader() { }
public string Username { get { return m_Username; } set { m_Username = value; } }
private string m_Username;
public string Password { get { return m_Password; } set { m_Password = value; } }
private string m_Password;
}
/// <summary>
/// Summary description for WebTest
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebTest : System.Web.Services.WebService
{
public UserHeader m_userHeader = null;
public UserHeader UserHeader
{
get { return m_userHeader; }
set { m_userHeader = value; }
}
[WebMethod]
[SoapHeader("UserHeader", Direction = SoapHeaderDirection.InOut)]
public string addTestlistItem()
{
try
{
Microsoft.SharePoint.Client.ClientContext cc = new Microsoft.SharePoint.Client.ClientContext("http://...");
cc.Credentials = new System.Net.NetworkCredential(m_userHeader.Username, m_userHeader.Password);
Web web = cc.Web;
List list = web.Lists.GetByTitle("TestList");
ListItemCreationInformation licci = new ListItemCreationInformation();
ListItem item = list.AddItem(licci);
item["Title"] = "12312313123123123";
item.Update();
cc.Load(item);
cc.ExecuteQuery();
}
catch (Exception ex)
{
return ex.Message;
}
return "succeed";
}
}
{
public UserHeader() { }
public string Username { get { return m_Username; } set { m_Username = value; } }
private string m_Username;
public string Password { get { return m_Password; } set { m_Password = value; } }
private string m_Password;
}
/// <summary>
/// Summary description for WebTest
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebTest : System.Web.Services.WebService
{
public UserHeader m_userHeader = null;
public UserHeader UserHeader
{
get { return m_userHeader; }
set { m_userHeader = value; }
}
[WebMethod]
[SoapHeader("UserHeader", Direction = SoapHeaderDirection.InOut)]
public string addTestlistItem()
{
try
{
Microsoft.SharePoint.Client.ClientContext cc = new Microsoft.SharePoint.Client.ClientContext("http://...");
cc.Credentials = new System.Net.NetworkCredential(m_userHeader.Username, m_userHeader.Password);
Web web = cc.Web;
List list = web.Lists.GetByTitle("TestList");
ListItemCreationInformation licci = new ListItemCreationInformation();
ListItem item = list.AddItem(licci);
item["Title"] = "12312313123123123";
item.Update();
cc.Load(item);
cc.ExecuteQuery();
}
catch (Exception ex)
{
return ex.Message;
}
return "succeed";
}
}
调用:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args)
{
WebTestSoapClient wtsc = new WebTestSoapClient();
UserHeader uh = new UserHeader();
uh.Username = "username";
uh.Password = "password";
Console.WriteLine(wtsc.addTestlistItem(ref uh));
Console.ReadLine();
}
{
WebTestSoapClient wtsc = new WebTestSoapClient();
UserHeader uh = new UserHeader();
uh.Username = "username";
uh.Password = "password";
Console.WriteLine(wtsc.addTestlistItem(ref uh));
Console.ReadLine();
}