0

I am overriding our global asax page where i want our users who are in a particular sharepoint list/ sharepoint group be redirected to the 2fa page (third party provided) every time the access sharepoint site. this is my code however i get a null reference exception error when i check the uls logs. this is a snippet of my code, this is the entry point:

public void Application_BeginRequest(object sender, EventArgs e)
        {
            //this.Context.Response.Redirect("https://www.google.com");
            if (SPContext.Current.Web.IsCurrentUserMemberOfGroup(Convert.ToInt32(WebConfigurationManager.AppSettings.Get("2FAAdminGroup"))))
            {
                if (this.Context.Session["jwt"] == null)
                {
                    this.Context.Session["jwt"] = "continue";
                    string text = SPContext.Current.Web.CurrentUser.LoginName;
                    if (text.Contains("\\"))
                    {
                        text = text.Substring(text.IndexOf("\\") + 1);
                    }
                    this.r1 = this.Status_Check(text);
                    if (string.IsNullOrEmpty(this.r1))
                    {
                        string url = this.Deregister_Check(text);
                        this.Context.Response.Redirect(url);
                    }
                    this.Context.Response.Redirect(this.r1);
                }
                else
                {
                    string a = this.Context.Session["jwt"].ToString();
                    if (a == "continue")
                    {
                        string text2 = HttpContext.Current.Request.QueryString["jwt"];
                        if (text2 != null && text2 != "")
                        {
                            this.Context.Session["jwt"] = text2;
                        }
                        else
                        {
                            this.Context.Session["jwt"] = "continue";
                            string text = SPContext.Current.Web.CurrentUser.LoginName;
                            if (text.Contains("\\"))
                            {
                                text = text.Substring(text.IndexOf("\\") + 1);
                            }
                            this.r1 = this.Status_Check(text);
                            if (string.IsNullOrEmpty(this.r1))
                            {
                                string url2 = this.Deregister_Check(text);
                                this.Context.Response.Redirect(url2);
                            }
                            this.Context.Response.Redirect(this.r1);
                        }
                    }
                }
            }     
        }

basically what it first does is it gets the data from the web config. but i am getting errors on that part i think here "SPContext.Current.Web.IsCurrentUserMemberOfGroup(Convert.ToInt32(WebConfigurationManager.AppSettings.Get("2FAAdminGroup")))"

i am new to this so can somebody shed some light on what method to use for this?

1 Answer 1

0

Can't init HttpContext.Current in Application_BeginRequest based on my testing.

You can get it in Application_PreRequestHandlerExecute.

Here is my test result(refer http://unabo.blogspot.com/2011/04/override-globalasax-in-sharepoint.html).

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
        {
            Microsoft.SharePoint.SPWeb contextWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext.Current);
            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    Microsoft.SharePoint.SPSite site = null;
                    Microsoft.SharePoint.SPWeb web = null;
                    using (site = new Microsoft.SharePoint.SPSite(contextWeb.Site.Url))
                    {
                        web = site.OpenWeb();
                    }
                });
        }

enter image description here

1
  • hi! @Lee_MSFT is it possible to also get data from the web config in pre request handler execute method? and i have a dumb question. how do i breakpoint this one? because i am building the project and copying the dll to the bin folder of my sharepoint root and i am not sure how to debug it.
    – Page F.P.T
    Commented Apr 2, 2019 at 11:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.