Xamarin.Auth iOS9 Authentication SSL ERROR

2019-04-30 19:45发布

问题:

I've just updated XCode to the 7.0 (7A220) and this take my Simulators to iOS9.

From that moment I cannot perform successfully any OAUTH call from the simulators.. I tried every model, from my App to the "sample Xamarin.Auth App".

The answer is always the same:

"Authentication Error

An SSL error has occurred and a secure connection to the server cannot be made"

The Code is the STANDARD one, I only changed my AppID. The same code is working on the Android version of the same App!

var auth = new OAuth2Authenticator (
            clientId: "my app id",
            scope: "",
            authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"),
            redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html"));

        auth.AllowCancel = allowCancel;

        // If authorization succeeds or is canceled, .Completed will be fired.
        auth.Completed += (s, e) =>
        {
            // We presented the UI, so it's up to us to dismiss it.
            dialog.DismissViewController (true, null);

            if (!e.IsAuthenticated) {
                facebookStatus.Caption = "Not authorized";
                dialog.ReloadData();
                return;
            }

            // Now that we're logged in, make a OAuth2 request to get the user's info.
            var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, e.Account);
            request.GetResponseAsync().ContinueWith (t => {
                if (t.IsFaulted)
                    facebookStatus.Caption = "Error: " + t.Exception.InnerException.Message;
                else if (t.IsCanceled)
                    facebookStatus.Caption = "Canceled";
                else
                {
                    var obj = JsonValue.Parse (t.Result.GetResponseText());
                    facebookStatus.Caption = "Logged in as " + obj["name"];
                }

                dialog.ReloadData();
            }, uiScheduler);
        };

        UIViewController vc = auth.GetUI ();
        dialog.PresentViewController (vc, true, null);

The IOS9 Simulator can surf the web, so it is not a "connectivity problem". I also tried with Facebook SDK, same error. Could it be a certificate issue?

Thanks

回答1:

To Fix this problem, simply add to your Info.plist file these lines:

<key>NSAppTransportSecurity</key>
  <dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
  <key>facebook.com</key>
  <dict>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSExceptionRequiresForwardSecrecy</key>
    <false/>
  </dict>
  <key>fbcdn.net</key>
  <dict>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSExceptionRequiresForwardSecrecy</key>
    <false/>
  </dict>
  <key>akamaihd.net</key>
  <dict>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSExceptionRequiresForwardSecrecy</key>
    <false/>
  </dict>
</dict>

If you don't care about extra rules for domains, you can simply add:

 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

NOTE: you have to Clean and Rebuild the project in order to see it running with these new settings!