无法使用Share Point 2013将身份验证凭据传递到客户端对象模型C#

时间:2016-05-04 18:04:05

标签: c# sharepoint sharepoint-2013

我正在尝试使用.NET C#将凭据传递给我的客户端对象模型。我正在使用Share Point 2013并使用Microsoft.SharePoint.Client.dll

我尝试了很多不同的迭代,但仍然遇到401 Unauthorized错误。但是 - 如果我通过浏览器登录网站URL - 我能够登录没问题 - 这意味着我有权访问该网站。下面是我使用的方法 - 以及代码更新。如果有人可以请一看,解释或告诉我如何正确地做到这一点,我将非常感激!

认证()

    public static ClientContext Authentication()
    {
        //Take 1:
        NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"https://CLARITYCON/");


        ClientContext clientContext = new ClientContext(siteURL);

        clientContext.Credentials = _myCredentials;

        return clientContext;

        //Take 2:
        //ClientContext clientContext = new ClientContext(siteURL);


        //SecureString passWord = new SecureString();

        //foreach (char c in passwordFixed.ToCharArray()) passWord.AppendChar(c);
        //clientContext.Credentials = new SharePointOnlineCredentials(userNameFixed, passWord);

        //try
        //{
        //    Console.WriteLine("Working!");
        //    return clientContext;
        //}
        //catch (Exception e)
        //{
        //    Console.WriteLine(e);
        //    return null;
        //}

        // Take 3:
        //using (ClientContext clientContext = new ClientContext(siteURL))
        //{
        //    SecureString passWord = new SecureString();

        //    foreach (char c in passwordFixed.ToCharArray()) passWord.AppendChar(c);

        //    clientContext.Credentials = new SharePointOnlineCredentials(userNameFixed, passWord);

        //    try
        //    {
        //        return clientContext;
        //    }

        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e);
        //        return null;
        //    }

        //}


    }

更新

我还添加了我正在调用的方法 - Authentication()

DeleteAFile()

    /// <summary>
    /// Will list out all the items within a Site, conduct a search and delete the item when found.
    /// </summary>
    /// <param name="sFileName"></param>
    /// <param name="sFldrLoc"></param>
    private static void DeleteAFile(string sFileName, string sFldrLoc)
    {

      var clientContext = Authentication();

        Web web = clientContext.Web;
        ListCollection collList = web.Lists;

        List oList = collList.GetByTitle(sFldrLoc);

        CamlQuery query = new CamlQuery();

        query.ViewXml = "<View><Query><Where><Leq>" +
            "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +
            "</Leq></Where></Query><RowLimit>50</RowLimit></View>";

        ListItemCollection collListItem = oList.GetItems(query);

        clientContext.Load(collListItem,
            items => items.IncludeWithDefaultProperties(
                item => item.DisplayName));
        clientContext.ExecuteQuery();


        foreach (ListItem listitem in collListItem)
        {
            if (listitem.DisplayName.Equals(sFileName))
            {
                listitem.DeleteObject();
                clientContext.ExecuteQuery();

                Console.WriteLine("{0}, has been deleted sucessfully!", listitem.DisplayName);
            }

        }
    }

评论代码是我厌倦实施的所有代码版本。所有人都返回401 Unauthorized错误。

1 个答案:

答案 0 :(得分:0)

您最常在最后一个参数中传递您的域名而不是您的Sharepoint网址

NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"https://CLARITYCON/");

如果&#34; CLARITYCON&#34;您的域名不是您的Sharepoint网址,您最常删除HTTPS,例如此代码:

NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"CLARITYCON");
此代码中的

DomainName \ UserName