0

UWP app connecting to Azure AD via IdentityModel.OidcClient generates an error as below.

Exception Message = "The specified protocol is unknown. (Exception from HRESULT: 0x800C000D)" Noting important in Stack Trace!

Exception happens inside public class WabBrowser : IBrowser's InvokeAsyncCore(BrowserOptions options, bool silentMode) function.

Code:

public class WabBrowser : IBrowser
    {
        private readonly bool _enableWindowsAuthentication;

        public WabBrowser(bool enableWindowsAuthentication = false)
        {
            _enableWindowsAuthentication = enableWindowsAuthentication;
        }

        private async Task<BrowserResult> InvokeAsyncCore(BrowserOptions options, bool silentMode)
        {
            var wabOptions = WebAuthenticationOptions.UseHttpPost;

            if (_enableWindowsAuthentication)
            {
                wabOptions |= WebAuthenticationOptions.UseCorporateNetwork;
            }
            if (silentMode)
            {
                wabOptions |= WebAuthenticationOptions.SilentMode;
            }

            WebAuthenticationResult wabResult;

            try
            {
                if (string.Equals(options.EndUrl, WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri, StringComparison.Ordinal))
                {
                    wabResult = await WebAuthenticationBroker.AuthenticateAsync(
                        wabOptions, new Uri(options.StartUrl));
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(options.EndUrl))
                    {
                        wabResult = await WebAuthenticationBroker.AuthenticateAsync(
                        wabOptions, new Uri(options.StartUrl), WebAuthenticationBroker.GetCurrentApplicationCallbackUri());
                    }
                    else
                    {
                        wabResult = await WebAuthenticationBroker.AuthenticateAsync(
                        wabOptions, new Uri(options.StartUrl), new Uri(options.EndUrl));
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.WriteErrorsToLogViaMessenger("WabBrowser-InvokeAsyncCore", ex);

                return new BrowserResult
                {
                    ResultType = BrowserResultType.UnknownError,
                    Error = ex.ToString()
                };
            }
}

This issue occurs only connecting to Azure AD and when connecting to other Identity servers this implementation works fine. Any help would be appreciated.

SurenSaluka
  • 1,534
  • 3
  • 18
  • 36

1 Answers1

0

The answer is simple. Instead of UseHttpPost in WebAuthenticationOptions, you have to set it to None when you're connecting to Azure AD.

var wabOptions = WebAuthenticationOptions.None;
SurenSaluka
  • 1,534
  • 3
  • 18
  • 36
  • Refer https://coderedirect.com/questions/661579/exception-thrown-when-webauthenticationbroker-receives-an-oauth2-callback for more details. – SurenSaluka Dec 06 '21 at 05:04