SharePoint Foundation 2010客户端对象模型身份验证

时间:2013-04-01 02:54:18

标签: asp.net sharepoint office365 client-object-model sharepoint-clientobject

我想通过传递身份验证信息来实现自定义日志记录,如下面的参考链接所示:

我正在使用下面的代码,但它不会自动登录,而只是弹出登录窗口输入用户名和密码。我想通过以编程方式传递凭证来自动登录。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Net;
using MSDN.Samples.ClaimsAuth;

namespace Sp_Ctx
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //if (args.Length < 1) { Console.WriteLine("SP_Ctx <url>"); return; }

            string targetSite = "https://mysite.sharepoint.com";//args[0];
            using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
            {
                if (ctx != null)
                {
                    ctx.Credentials = new NetworkCredential("guest@mysite.com.au", "password", "mysite.sharepoint.com");
                    ctx.Load(ctx.Web); // Query for Web
                    ctx.ExecuteQuery(); // Execute
                    Console.WriteLine(ctx.Web.Title);
                }
            }
            Console.ReadLine();
        }
    }
}

更新

我托管了 MS 365 sharepoint 2013网站,但我想使用版本2010身份验证机制。

2 个答案:

答案 0 :(得分:0)

您正在使用的MSDN示例项目仅允许通过弹出窗口进行基于声明的身份验证。为了以编程方式执行此操作,我使用了另一个帮助程序库,WictorWilén的claims demo。然后,您可以使用他的MsOnlineClaimsHelper类进行身份验证。以下是直接从Wictor博客中获取的示例代码:

MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper(url, username, password);    

using (ClientContext context = new ClientContext(url)) {

  context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;

  context.Load(context.Web);

  context.ExecuteQuery();

  Console.WriteLine("Name of the web is: " + context.Web.Title);

}

这是我六个月前为一个项目所做的。但是,我很想知道是否有新的最佳实践或微软提供的东西。

答案 1 :(得分:0)

试试这个:

ctx.Credentials = new NetworkCredential("guest", "password", "mysite.com.au");

凭据中的域应该是来宾帐户的域而不是sharepoint服务器名称。

相关问题