protected override Task ApplyResponseChallengeAsync()
        {
            if (Response.StatusCode != 401)
            {
                return(Task.FromResult <object>(null));
            }

            AuthenticationResponseChallenge challenge =
                Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);

            if (challenge != null)
            {
                string baseUri =
                    Request.Scheme +
                    Uri.SchemeDelimiter +
                    Request.Host +
                    Request.PathBase;

                string currentUri =
                    baseUri +
                    Request.Path +
                    Request.QueryString;

                string redirectUri =
                    baseUri +
                    Options.CallbackPath;

                AuthenticationProperties properties = challenge.Properties;
                if (string.IsNullOrEmpty(properties.RedirectUri))
                {
                    properties.RedirectUri = currentUri;
                }

                // OAuth2 10.12 CSRF
                GenerateCorrelationId(properties);

                var queryStrings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                queryStrings.Add("responseType", "code");
                queryStrings.Add("clientId", Options.ClientId);
                queryStrings.Add("redirectUri", redirectUri);

                // default scope
                if (Options.Scope.Count == 0)
                {
                    Options.Scope.Add(DefaultScope);
                }
                AddQueryString(queryStrings, properties, "scope", string.Join(",", Options.Scope));

                if (!String.IsNullOrEmpty(Options.AppName))
                {
                    AddQueryString(queryStrings, properties, "appName", Options.AppName);
                }
                if (!String.IsNullOrEmpty(Options.AppLogoUrl))
                {
                    AddQueryString(queryStrings, properties, "appLogoUrl", Options.AppLogoUrl);
                }

                string state = Options.StateDataFormat.Protect(properties);
                queryStrings.Add("state", state);

                string authorizationEndpoint = WebUtilities.AddQueryString(AuthorizeEndpoint, queryStrings);

                var redirectContext = new BlueJeansApplyRedirectContext(
                    Context, Options,
                    properties, authorizationEndpoint);
                Options.Provider.ApplyRedirect(redirectContext);
            }

            return(Task.FromResult <object>(null));
        }
 /// <summary>
 /// Called when a Challenge causes a redirect to authorize endpoint in the BlueJeans 2.0 middleware
 /// </summary>
 /// <param name="context">Contains redirect URI and <see cref="AuthenticationProperties"/> of the challenge </param>
 public virtual void ApplyRedirect(BlueJeansApplyRedirectContext context)
 {
     OnApplyRedirect(context);
 }