0
[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

is the message I'm getting when I deploy my website online.

Locally everything is running fine, even when I copy all project files to another computer everything shows fine.

However, when I publish the site to the IIS server it shows the error as displayed above.

Are the any settings which I could be missing? I doubt this is a coding issue as everything works perfectly on the localhost

code in 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;
            }"

Additional code for 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;
        }
    }
4
  • 1
    There's a bit of a code here, would be useful to tell us where you're getting this error. Commented Nov 1, 2012 at 13:55
  • @GrantThomas see first post, the exact error is on top. thanks in advance Commented Nov 1, 2012 at 14:02
  • 1
    OKay, well that tells you where the error is happening exactly; it's easier for you because you have the source in the editor + line counts etc. but something in the getCookieContainer is null and you're trying to do something with it. Can getSamlToken(); return null? If so then accessing cookies.FedAuth will break - use that as an example and check things aren't nothing (i.e. if (something != null). Commented Nov 1, 2012 at 14:05
  • @GrantThomas getSamlToken() throws this exception Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Commented Nov 1, 2012 at 14:36

2 Answers 2

2

Microsoft.EntityModel.dll was not added on the server on publish. I've put this .dll manually on the server and now it works.

Thanks for your help GrantThomas.

Sign up to request clarification or add additional context in comments.

Comments

0

It is actually Microsoft.IdentityModel reference with path:

C:\Program Files\Reference Assemblies\Microsoft\Windows Identity Foundation\v3.5\Microsoft.IdentityModel.dll

if you have a VS installed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.