Monday, September 16, 2019

SharePoint Provider Hosted App: AuthenticationContext.HttpContext.User is Null after SharePointContextProvider.CheckRedirectionStatus

The issue was AuthenticationContext.HttpContext.User object is Null after SharePointContextProvider.CheckRedirectionStatus call inside OnAuthentication() method of custom ActionFilterAttribute of a provider hosted app.

public void OnAuthentication(AuthenticationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            bool ok = false;
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(filterContext.HttpContext, out redirectUrl))
            {
                case RedirectionStatus.Ok:
                    ok = true;
                    break;
                case RedirectionStatus.ShouldRedirect:
                    filterContext.Result = new RedirectResult(redirectUrl.AbsoluteUri);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    filterContext.Result = new ViewResult { ViewName = "Error" };
                    break;
            }
            if (ok)
            {
 
                filterContext.Principal = filterContext.HttpContext.User;


Reason for this error was that the app was not registered in the dev server. After installing the app using AppRegNew.aspx this error was solved.

No comments:

c# httpclient The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

 If we get this error while trying to get http reponse using HttpClient object, it could mean that certificate validation fails for the remo...