/// <summary>
        /// Performs the app launch flow for the current request
        /// </summary>
        /// <param name="args">An <see cref="AppLauncherFunctionArgs"/> instance specifying the location of the client configuration in Azure storage.</param>
        /// <returns>If launch succeeds the response is a 302 redirect back to the SharePoint site's home page.</returns>
        public HttpResponseMessage Execute(AppLauncherFunctionArgs args)
        {
            try
            {
                _clientClientConfiguration = GetConfiguration(ClientId, args.StorageAccount, args.StorageAccountKey);
                var spContextToken = TokenHelper.ReadAndValidateContextToken(ContextToken, _requestAuthority, ClientId,
                                                                             _clientClientConfiguration.AcsClientConfig.ClientSecret);
                var spHostUri = new Uri(SPWebUrl);

                var accessToken = TokenHelper.GetACSAccessTokens(spContextToken, spHostUri.Authority,
                                                                 _clientClientConfiguration.ClientId,
                                                                 _clientClientConfiguration.AcsClientConfig.ClientSecret);


                var ctx = ConnectToSPWeb(accessToken);

                var securityTokens = new SecurityTokens()
                {
                    ClientId           = ClientId,
                    AccessToken        = accessToken.AccessToken,
                    AccessTokenExpires = accessToken.ExpiresOn,
                    AppWebUrl          = SPWebUrl,
                    Realm        = spContextToken.Realm,
                    RefreshToken = spContextToken.RefreshToken
                };

                var encodedCacheKey = TokenHelper.Base64UrlEncode(spContextToken.CacheKey);
                Log($"Storing tokens for {ClientId}/{encodedCacheKey}");
                StoreSecurityTokens(securityTokens, encodedCacheKey, args.StorageAccount, args.StorageAccountKey);

                Log($"Ensuring web properties for {ctx.Web.Url}");
                EnsureBaseConfiguration(encodedCacheKey);

                Log($"Sending app launch event for {ctx.Web.Url}");
                SendQueueMessage(new QueuedAppLaunchEvent()
                {
                    ClientId        = ClientId,
                    AppWebUrl       = ctx.Web.Url,
                    UserAccessToken = securityTokens.AccessToken,
                    AppAccessToken  = GetACSAccessTokens(ClientId, encodedCacheKey, true),
                    RetryCount      = 5
                });

                _response.StatusCode = HttpStatusCode.Moved;
                // TODO: add Doug worthy validation on SPHostUrl, whatever that means
                _response.Headers.Location = new Uri($"{ctx.Web.Url}?cId={ClientId}&cKey={encodedCacheKey}&SPHostUrl={_queryParams["SPHostUrl"]}");

                return(_response);
            }
            catch (Exception ex)
            {
                _response.StatusCode = HttpStatusCode.OK;
                _response.Content    = new StringContent(GetErrorPage(ex.ToString()));
                _response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
                return(_response);
            }
        }
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "AppLaunch")] HttpRequestMessage req, TraceWriter log)
        {
            Log(log, $"C# HTTP trigger function processed a request! RequestUri={req.RequestUri}");
            var func = new AppLaunchHandler(req);

            func.FunctionNotify += (sender, args) => Log(log, args.Message);

            var appLauncherFunctionArgs = new AppLauncherFunctionArgs
            {
                StorageAccount    = ConfigurationManager.AppSettings["ConfigurationStorageAccount"],
                StorageAccountKey = ConfigurationManager.AppSettings["ConfigurationStorageAccountKey"]
            };

            return(await Task.Run(() => func.Execute(appLauncherFunctionArgs)));
        }
Exemplo n.º 3
0
        public static Task <HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
        {
            Log(log, $"C# HTTP trigger function processed a request! RequestUri={req.RequestUri}");
            var func = new AppLaunchHandler(req);

            func.FunctionNotify += (sender, args) => Log(log, args.Message);

            var appLauncherFunctionArgs = new AppLauncherFunctionArgs()
            {
                StorageAccount    = ConfigurationManager.AppSettings["ConfigurationStorageAccount"],
                StorageAccountKey = ConfigurationManager.AppSettings["ConfigurationStorageAccountKey"]
            };

            return(Task.FromResult(func.Execute(appLauncherFunctionArgs)));
        }