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

时间:2022-01-28 16:55:08
[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.

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

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

我可能会丢失任何设置吗?我怀疑这是一个编码问题,因为一切都在localhost上完美运行

code in Default.aspx.cs:

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

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 个解决方案

#1


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.

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

Thanks for your help GrantThomas.

感谢您的帮助GrantThomas。

#2


0  

It is actually Microsoft.IdentityModel reference with path:

它实际上是具有路径的Microsoft.IdentityModel引用:

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

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

if you have a VS installed

如果你安装了VS.

#1


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.

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

Thanks for your help GrantThomas.

感谢您的帮助GrantThomas。

#2


0  

It is actually Microsoft.IdentityModel reference with path:

它实际上是具有路径的Microsoft.IdentityModel引用:

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

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

if you have a VS installed

如果你安装了VS.