通过WinForm应用程序使用特定的用户名/密码连接到Sharepoint站点

时间:2010-11-21 08:37:21

标签: c# winforms sharepoint

干草, 我正在开发一个连接到sharepoint站点并从中读取数据的Windows窗体应用程序(C#)(列表),

问题是: 如何使用特定的用户名/密码连接到站点,而不是使用当前的Windows用户或域用户。

请咨询

修改:P.S。:在SP 2007上工作

我的代码:

 SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite sourceSite = new SPSite(txtBoxSP.Text))
            {
                SPWeb sourceWeb = sourceSite.RootWeb;
                SPUserCollection usrs = sourceWeb.AllUsers;

                SPListCollection col = sourceWeb.GetListsOfType(SPBaseType.GenericList);

                foreach (SPList list in col)
                    System.Diagnostics.Debug.WriteLine(string.Format("My Name: {0}, Type: {1}, Length: {2}",
                        list.Title, list.GetType().ToString(), list.ItemCount.ToString()));
            }
        });

2 个答案:

答案 0 :(得分:4)

像这样:

var cc = new CredentialCache();
cc.Add(
    new Uri(sourceSite.Url), 
    "NTLM", 
    new NetworkCredential("user", "password"));
sourceSite.Credentials = cc;

当然,对这样的密码进行硬编码并不是很安全。您可以向用户询问相关信息。 NetworkCredential有另一个constructor为此目的使用安全字符串。


更新:

我没有注意到您使用的是SharePoint对象模型。最初我以为你是从WSDL生成一个代理。 Here's an example如何实现这一点。

答案 1 :(得分:0)

也许这篇文章可以帮到你

MSDN 在此页面中查找给定的行。 http://msdn.microsoft.com/en-us/library/ff829215.aspx我认为这篇文章会告诉你一个方法。告诉我,如果你找不到这些行:)

Connection Strings

There may be situations where you need to display data from databases or REST and SOAP services. Accessing data from these data sources requires either connection strings or at least the URL and endpoints of the services to be stored somewhere. Web.config files are an obvious choice but using Web.config files in SharePoint has some caveats. 
The first differentiation we have to make is whether or not we are developing a sandboxed solution. Let’s look at the farm solution first. Farm solutions have access to the file system. Because every site collection is hosted in a web application and every web application has a Web.config file, the Web.config file associated with your farm solution is one place you can store connection strings or other data. The method for doing this is similar to how it is done in ASP.NET development.