[NullReferenceException:对象引用未设置为对象的实例。]

时间:2012-11-01 13:53:16

标签: asp.net sharepoint nullreferenceexception

[NullReferenceException: Object reference not set to an instance of an object.]
   Wictor.Office365.MsOnlineClaimsHelper.getCookieContainer() +128
   Wictor.Office365.MsOnlineClaimsHelper.clientContext_ExecutingWebRequest(Object sender, WebRequestEventArgs e) +33
   Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() +382
   Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() +16
   Webapplicatie.Default.Page_Load(Object sender, EventArgs e) +334
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3177

是我在线部署网站时收到的消息。

本地一切运行正常,即使我将所有项目文件复制到另一台计算机,一切都很好。

但是,当我将站点发布到IIS服务器时,它会显示上面显示的错误。

我可能遗失的任何设置? 我怀疑这是一个编码问题,因为一切都在localhost

上完美运行 Default.aspx.cs中的

代码:

  MsOnlineClaimsHelper claimsHelper = new MsOnlineClaimsHelper(sharepointsiteUrl, username, password);
            using (ClientContext context = new ClientContext(sharepointsiteUrl))
            {
                context.ExecutingWebRequest += claimsHelper.clientContext_ExecutingWebRequest;

                context.Load(context.Web);

                context.ExecuteQuery();

                Lebel.Text = "Succesfully logged in as " + username + " on " + context.Web.Title;
            }"

MsOnlineClaimsHelper的附加代码

// Method used to add cookies to CSOM
    public void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e) {
        e.WebRequestExecutor.WebRequest.CookieContainer = getCookieContainer();
        //e.WebRequestExecutor.WebRequest.UserAgent = userAgent;
    }

    // Creates or loads cached cookie container
    CookieContainer getCookieContainer() {
        if (_cachedCookieContainer == null || DateTime.Now > _expires) {

            // Get the SAML tokens from SPO STS (via MSO STS) using fed auth passive approach
            MsoCookies cookies = getSamlToken();

            if (!string.IsNullOrEmpty(cookies.FedAuth)) {

                // Create cookie collection with the SAML token                    
                _expires = cookies.Expires;
                CookieContainer cc = new CookieContainer();

                // Set the FedAuth cookie
                Cookie samlAuth = new Cookie("FedAuth", cookies.FedAuth) {
                    Expires = cookies.Expires,
                    Path = "/",
                    Secure = cookies.Host.Scheme == "https",
                    HttpOnly = true,
                    Domain = cookies.Host.Host
                };
                cc.Add(samlAuth);


                if (_useRtfa) {
                    // Set the rtFA (sign-out) cookie, added march 2011
                    Cookie rtFa = new Cookie("rtFA", cookies.rtFa) {
                        Expires = cookies.Expires,
                        Path = "/",
                        Secure = cookies.Host.Scheme == "https",
                        HttpOnly = true,
                        Domain = cookies.Host.Host
                    };
                    cc.Add(rtFa);
                }
                _cachedCookieContainer = cc;
                return cc;
            }
            return null;
        }
        return _cachedCookieContainer;
    }

    public CookieContainer CookieContainer {
        get {
            if (_cachedCookieContainer == null || DateTime.Now > _expires) {
                return getCookieContainer();
            }
            return _cachedCookieContainer;
        }
    }

2 个答案:

答案 0 :(得分:2)

发布时未在服务器上添加Microsoft.EntityModel.dll。 我已经将这个.dll手动放在服务器上了,现在它可以工作了。

感谢你帮助GrantThomas。

答案 1 :(得分:0)

实际上是带路径的Microsoft.IdentityModel引用:

  

C:\ Program Files \ Reference Assemblies \ Microsoft \ Windows Identity   基础\ V3.5 \ Microsoft.IdentityModel.dll

如果您安装了VS

相关问题