protected virtual AppPrincipal FindExistingAppPrincipal(WebModelHost webHost, AppPrincipalDefinition appPrincipalModel)
        {
            var context = webHost.HostWeb.Context;
            //var context = webHost.HostclientContext;

            var appPrincipalManager  = AppPrincipalManager.GetManager(context, webHost.HostWeb);
            var appPrincipalProvider = AppPrincipalIdentityProvider.GetExternal(context);

            var appPrincipalName = AppPrincipalName.CreateFromAppPrincipalIdentifier(context, appPrincipalModel.AppId);
            var result           = appPrincipalManager.LookupAppPrincipal(appPrincipalProvider, appPrincipalName);

            context.Load(result);
            context.ExecuteQueryWithTrace();

            return(result);
        }
        private void DeployAppPrincipal(object modelHost, WebModelHost webHost, AppPrincipalDefinition appPrincipalModel)
        {
            var context = webHost.HostWeb.Context;

            var appPrincipalManager = AppPrincipalManager.GetManager(context, webHost.HostWeb);
            var principal           = FindExistingAppPrincipal(webHost, appPrincipalModel);

            context.ExecuteQueryWithTrace();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = principal,
                ObjectType       = typeof(AppPrincipal),
                ObjectDefinition = appPrincipalModel,
                ModelHost        = modelHost
            });


            if (principal == null)
            {
                var endpoints = new List <string>();
                endpoints.Add(appPrincipalModel.RedirectURI);

                var now = DateTime.Now;

                var credential = AppPrincipalCredential.CreateFromSymmetricKey(context, appPrincipalModel.AppSecret, now, now.AddYears(1));

                var externalAppPrincipalCreationParameters = new ExternalAppPrincipalCreationParameters()
                {
                    AppIdentifier = appPrincipalModel.AppId,
                    DisplayName   = appPrincipalModel.Title,
                    Credential    = credential,
                    ApplicationEndpointAuthorities = endpoints
                };

                principal = appPrincipalManager.CreateAppPrincipal(externalAppPrincipalCreationParameters);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = principal,
                    ObjectType       = typeof(AppPrincipal),
                    ObjectDefinition = appPrincipalModel,
                    ModelHost        = modelHost
                });
            }
            else
            {
                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = principal,
                    ObjectType       = typeof(AppPrincipal),
                    ObjectDefinition = appPrincipalModel,
                    ModelHost        = modelHost
                });
            }
        }