/// <summary> /// Handles Challenge /// </summary> /// <returns></returns> protected override async Task ApplyResponseChallengeAsync() { if (Response.StatusCode == 401) { AuthenticationResponseChallenge challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode); if (challenge == null) { return; } string baseUri = Request.Scheme + Uri.SchemeDelimiter + Request.Host + Request.PathBase; string currentUri = baseUri + Request.Path + Request.QueryString; // Save the original challenge URI so we can redirect back to it when we're done. AuthenticationProperties properties = challenge.Properties; if (string.IsNullOrEmpty(properties.RedirectUri)) { properties.RedirectUri = currentUri; } WsFederationMessage wsFederationMessage = new WsFederationMessage() { IssuerAddress = Options.IssuerAddress ?? string.Empty, Wtrealm = Options.Wtrealm, Wctx = WsFederationAuthenticationDefaults.WctxKey + "=" + Uri.EscapeDataString(Options.StateDataFormat.Protect(properties)), }; if (!string.IsNullOrWhiteSpace(Options.Wreply)) { wsFederationMessage.Wreply = Options.Wreply; } if (Options.Notifications != null && Options.Notifications.RedirectToIdentityProvider != null) { RedirectToIdentityProviderNotification <WsFederationMessage> notification = new RedirectToIdentityProviderNotification <WsFederationMessage> { ProtocolMessage = wsFederationMessage }; await Options.Notifications.RedirectToIdentityProvider(notification); if (notification.Cancel) { return; } } string redirect = wsFederationMessage.CreateSignInQueryString(); if (!Uri.IsWellFormedUriString(redirect, UriKind.Absolute)) { _logger.WriteError(string.Format(CultureInfo.InvariantCulture, "The WsFederation sign-in redirect uri is not well formed: '{0}'", redirect)); return; } Response.Redirect(redirect); } return; }