Unable to debug my Remote Event Reciever locally using Ngrok

john john 926 Reputation points
2021-04-08T16:08:48.85+00:00

Last month the below steps were working well for me to debug and test a remote event receiver locally:-

1) Open Ngrok.exe >> run the following command inside ngrok:-

ngrok authtoken 3***e  
ngrok http --host-header=rewrite  57269  

2) register a new app:- @ https://****.sharepoint.com/sites/****/_layouts/15/AppRegNew.aspx >> enter the above Ngrok.exe urls inside the App Redirect URL & App Domain

3) Inside the "_layouts/15/appinv.aspx" >> i search for the above app using client ID >> and enter the following:-

<AppPermissionRequests AllowAppOnlyPolicy="true">  
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />  
</AppPermissionRequests>  

5) update the service 's web config with the above client id and client secret

6) register the new remove event receiver as follow:-

Add-PnPEventReceiver -List "Order Management" -Name "TasksRER" -Url http://cc6e945e82f6.ngrok.io/service1.svc -EventReceiverType ItemUpdated -Synchronization Asynchronous  

but today when i tried the above steps it failed >> where inside my event receiver when i tried to get the context >> i will get that the Context is null:-

 public void ProcessOneWayEvent(SPRemoteEventProperties properties)  
        {  
            var prop = properties;  
            var listItemID = properties.ItemEventProperties.ListItemId;  
            var listTitle = properties.ItemEventProperties.ListTitle;  
            using (ClientContext context = Helpers.GetAppOnlyContext(properties.ItemEventProperties.WebUrl))  
            {  
                context.Load(context.Web);  
                context.ExecuteQuery();  

Here is a screen shot from Visual Studio with the error i am getting when trying to get the context:-

85719-exce.png

Any advice if anything has been changed which is preventing me from running the above steps? which were working well last month?
Thanks

here is the code for the GetAppOnlyContext

    public class Helpers  
        {  
            public static ClientContext GetAppOnlyContext(string siteUrl)  
            {  
                try  
                {  
                    Uri siteUri = new Uri(siteUrl);  
                    string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);  
                    string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;  

                    return TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken);  
                }  

                catch (Exception ex)  
                {  
                    Trace.TraceInformation("GetAppOnlyContext failed. {0}", ex.Message);  
                }  
                return null;  
            }  

            public static ClientContext GetAuthenticatedContext(string siteUrl)  
            {  
                string userName = WebConfigurationManager.AppSettings.Get("AuthenticatedUserName");  
                string password = WebConfigurationManager.AppSettings.Get("AuthenticatedUserPassword");  
                return GetAuthenticatedContext(siteUrl, userName, password);  
            }  

            public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, SecureString password)  
            {  
                ClientContext ctx = new ClientContext(siteUrl);  
                ctx.Credentials = new SharePointOnlineCredentials(userName, password);  
                return ctx;  
            }  

            public static ClientContext GetAuthenticatedContext(string siteUrl, string userName, string password)  
            {  
                SecureString securePassword = GetPassword(password);  
                return GetAuthenticatedContext(siteUrl, userName, securePassword);  
            }  

            private static SecureString GetPassword(string passwd)  
            {  
                var secure = new SecureString();  
                foreach (char c in passwd)  
                {  
                    secure.AppendChar(c);  
                }  
                return secure;  
            }  

            public static string EmptyIfNull(object obj)  
            {  
                return obj == null ? "" : obj.ToString();  
            }  
        }  
    }    
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,561 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jerryzy 10,561 Reputation points
    2021-04-09T06:06:09.817+00:00

    What's the details of Helpers.GetAppOnlyContext() ? Seems it's a custom method and a custom Helpers class.