TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase> method

Future<bool> TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase>(
  1. TSettings newSettings,
  2. String emailAddress,
  3. Uri redirectionUrl,
  4. OutParam<TSettings> settings,
)
Get an autodiscover SRV record in DNS and construct autodiscover URL. Name of the domain. The email address. Redirection Url. The settings.

Implementation

//        Uri GetRedirectionUrlFromDnsSrvRecord(String domainName)
//        {
//            this.TraceMessage(
//                TraceFlags.AutodiscoverConfiguration,
//                string.Format("Trying to get Autodiscover host from DNS SRV record for {0}.", domainName));
//
//            String hostname = this.dnsClient.FindAutodiscoverHostFromSrv(domainName);
//            if (!StringUtils.IsNullOrEmpty(hostname))
//            {
//                this.TraceMessage(
//                    TraceFlags.AutodiscoverConfiguration,
//                    string.Format("Autodiscover host {0} was returned.", hostname));
//
//                return new Uri(string.Format(AutodiscoverLegacyHttpsUrl, hostname));
//            }
//            else
//            {
//                this.TraceMessage(
//                    TraceFlags.AutodiscoverConfiguration,
//                    "No matching Autodiscover DNS SRV records were found.");
//
//                return null;
//            }
//        }

/// <summary>

/// </summary>
/// <typeparam name="TSettings">The type of the settings.</typeparam>
/// <param name="emailAddress">The email address.</param>
/// <param name="redirectionUrl">Redirection Url.</param>
/// <param name="settings">The settings.</param>
/* private */
Future<bool>
    TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase>(
        TSettings newSettings,
        String emailAddress,
        Uri redirectionUrl,
        OutParam<TSettings> settings) async {
  settings.param = null;

  List<String> redirectionEmailAddresses = <String>[];

  // Bug 60274: Performing a non-SSL HTTP GET to retrieve a redirection URL is potentially unsafe. We allow the caller
  // to specify delegate to be called to determine whether we are allowed to use the redirection URL.
  if (this.CallRedirectionUrlValidationCallback(redirectionUrl.toString())) {
    for (int currentHop = 0;
        currentHop < AutodiscoverService.AutodiscoverMaxRedirections;
        currentHop++) {
      try {
        settings.param = this._GetLegacyUserSettingsAtUrl<TSettings>(
            newSettings, emailAddress, redirectionUrl);

        switch (settings.param!.ResponseType) {
          case AutodiscoverResponseType.Success:
            return true;
          case AutodiscoverResponseType.Error:
            throw new AutodiscoverRemoteException(
                "Strings.AutodiscoverError", settings.param!.Error);
          case AutodiscoverResponseType.RedirectAddress:
            // If this email address was already tried, we may have a loop
            // in SCP lookups. Disable consideration of SCP records.
            this._DisableScpLookupIfDuplicateRedirection(
                settings.param!.RedirectTarget!, redirectionEmailAddresses);

            settings.param = await this
                .InternalGetLegacyUserSettings<TSettings>(
                    newSettings,
                    settings.param!.RedirectTarget!,
                    redirectionEmailAddresses,
                    OutParam()..param = currentHop);
            return true;
          case AutodiscoverResponseType.RedirectUrl:
            try {
              redirectionUrl = Uri.parse(settings.param!.RedirectTarget!);
            } catch (UriFormatException) {
              this.TraceMessage(
                  enumerations.TraceFlags.AutodiscoverConfiguration,
                  "Service returned invalid redirection URL ${settings.param!.RedirectTarget}");
              return false;
            }
            break;
          default:
            String failureMessage =
                "Autodiscover call at $redirectionUrl failed with error ${settings.param!.ResponseType}, target ${settings.param!.RedirectTarget}";
            this.TraceMessage(
                enumerations.TraceFlags.AutodiscoverConfiguration,
                failureMessage);
            return false;
        }
      } on WebException catch (ex) {
        if (ex.Response != null) {
          IEwsHttpWebResponse response =
              this.HttpWebRequestFactory.CreateExceptionResponse(ex);
          if (this._TryGetRedirectionResponse(
              response, OutParam()..param = redirectionUrl)) {
            this.TraceMessage(
                enumerations.TraceFlags.AutodiscoverConfiguration,
                "Host returned a redirection to url ${redirectionUrl}");
            continue;
          } else {
            this.ProcessHttpErrorResponse(response, ex);
          }
        }

        this.TraceMessage(enumerations.TraceFlags.AutodiscoverConfiguration,
            "$_url failed: ${ex.runtimeType} (${ex.message})");

        return false;
      } on XmlException catch (ex) {
        // If the response is malformed, it wasn't a valid Autodiscover endpoint.
        this.TraceMessage(enumerations.TraceFlags.AutodiscoverConfiguration,
            "$redirectionUrl failed: XML parsing error: $ex");
        return false;
      } on IOException catch (ex) {
        this.TraceMessage(enumerations.TraceFlags.AutodiscoverConfiguration,
            "$redirectionUrl failed: I/O error: $ex");
        return false;
      }
    }
  }

  return false;
}