xamarin ios app is not returning after Azure AD B2

2019-08-26 09:18发布

问题:

I added a Native app in my B2C Tenant.

App.xaml.cs code looks like this

public static PublicClientApplication PCA = null;

// Azure AD B2C Coordinates
public static string Tenant = "something.onmicrosoft.com";
public static string ClientID = "*********-0e3a-483a-9e15-7896c524ed1d";
public static string PolicySignUpSignIn = "B2C_1_EmailSignup";
public static string PolicyEditProfile = "B2C_1_PasswordReset";
public static string PolicyResetPassword = "B2C_1_PasswordReset";

public static string[] Scopes = { "https://havethat.onmicrosoft.com/demoapi/demo.read" };
public static string ApiEndpoint = "https://havethat.azurewebsites.net/hello";

public static string AuthorityBase = $"https://login.microsoftonline.com/tfp/{Tenant}/";
public static string Authority = $"{AuthorityBase}{PolicySignUpSignIn}";
public static string AuthorityEditProfile = $"{AuthorityBase}{PolicyEditProfile}";
public static string AuthorityPasswordReset = $"{AuthorityBase}{PolicyResetPassword}";

public static UIParent UiParent = null;

public App()
{
    // default redirectURI; each platform specific project will have to override it with its own
    PCA = new PublicClientApplication(ClientID, Authority);
    PCA.RedirectUri = $"msal{ClientID}://auth";

    MainPage = new NavigationPage(new MainPage());
}

info.plist file has the following entry for App ID

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>URL Type 1</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>msal********-0e3a-483a-9e15-7896c524ed1d</string>
        </array>
    </dict>
</array>

on a simple click of a button i have just this code

async void OnSign(object sender, EventArgs e)
{
    try
    {
        AuthenticationResult ar = await App.PCA.AcquireTokenAsync(App.Scopes, GetUserByPolicy(App.PCA.Users, App.PolicySignUpSignIn), App.UiParent);
        UpdateUserInfo(ar);
    }
    catch (Exception ex)
    {

    }
}

now this code takes me to B2C Login page. after successful login it just stays there. If i press "Done" then it goes back to the app with exception that user cancels authenitcation. Shouldn't it just come back to the app after successful login ?

回答1:

iOS specific considerations

UserDetailsClient.iOS only requires one extra line, in AppDelegate.cs. You need to ensure that the OpenUrl handler looks as in snippet below:

csharp public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { 
    AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url, ""); 
    return true; 
} 

Once again, this logic is meant to ensure that once the interactive portion of the authentication flow is concluded, the flow goes back to MSAL.