Microsoft用于Xamarin Forms的active-directory-xamarin-native-v2示例项目无法在iOS中运行

时间:2019-06-04 13:58:18

标签: ios xamarin.forms

我正在研究office365身份验证,从github那里获取了代码,对于android构建来说,它工作正常,但是在iOS中无法从office365获得数据响应(令牌)。

项目链接-https://github.com/Azure-Samples/active-directory-xamarin-native-v2

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    async void OnSignInSignOut(object sender, EventArgs e)
    {
        AuthenticationResult authResult = null;
        IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();
        try
        {
            if (btnSignInSignOut.Text == "Sign in")
            {
                // let's see if we have a user in our belly already
                try
                {
                    IAccount firstAccount = accounts.FirstOrDefault();
                    authResult = await App.PCA.AcquireTokenSilent(App.Scopes, firstAccount)
                                          .ExecuteAsync();
                    await RefreshUserDataAsync(authResult.AccessToken).ConfigureAwait(false);
                    Device.BeginInvokeOnMainThread(() => { btnSignInSignOut.Text = "Sign out"; });
                }
                catch (MsalUiRequiredException ex)
                {
                    try
                    {

                        authResult = await App.PCA.AcquireTokenInteractive(App.Scopes)
                                                  .WithParentActivityOrWindow(App.ParentWindow)
                                                  .ExecuteAsync();

                        await RefreshUserDataAsync(authResult.AccessToken);
                        Device.BeginInvokeOnMainThread(() => { btnSignInSignOut.Text = "Sign out"; });
                    }
                    catch(Exception ex2)
                    {
                        slUser.IsVisible = true;
                    }
                }
            }
            else
            {
                while (accounts.Any())
                {
                    await App.PCA.RemoveAsync(accounts.FirstOrDefault());
                    accounts = await App.PCA.GetAccountsAsync();
                }

                slUser.IsVisible = false;
                Device.BeginInvokeOnMainThread(() => { btnSignInSignOut.Text = "Sign in"; });
            }
        }
        catch (Exception ex)
        {

        }
    }

    public async Task RefreshUserDataAsync(string token)
    {
        //get data from API
        slUser.IsVisible = true;
        HttpClient client = new HttpClient();
        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
        message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
        HttpResponseMessage response = await client.SendAsync(message);
        string responseString = await response.Content.ReadAsStringAsync();
        if (response.IsSuccessStatusCode)
        {
            JObject user = JObject.Parse(responseString);

            slUser.IsVisible = true;

            Device.BeginInvokeOnMainThread(() =>
            {

                lblDisplayName.Text = user["displayName"].ToString();
                lblGivenName.Text = user["givenName"].ToString();
                lblId.Text = user["id"].ToString();
                lblSurname.Text = user["surname"].ToString();
                lblUserPrincipalName.Text = user["userPrincipalName"].ToString();

                // just in case
                btnSignInSignOut.Text = "Sign out";
            });
        }
        else
        {
            await DisplayAlert("Something went wrong with the API call", responseString, "Dismiss");
        }
    }
}
}

我应该获得与Android构建相同的版本,但是在iOS中没有从office365服务器获取令牌,这似乎是证书问题,为了在iOS中工作,我发现需要遵循以下几行内容

此外,为了使令牌缓存正常工作并使AcquireTokenSilentAsync工作,必须遵循多个步骤:

1)在Entitlements.plist文件中启用钥匙串访问,并在钥匙串组中指定捆绑包标识符。

2)在项目选项的iOS捆绑签名视图上,为“自定义权利”字段选择Entitlements.plist文件。

3)在签署证书时,请确保XCode使用相同的Apple ID。

以上均来自Microsoft网站

下面是Entitlements.plist文件的详细信息- 钥匙串访问组我正在使用$(AppIdentifierPrefix)com.oauth.office365,其中com.oauth.office365是我的捆绑包标识符

0 个答案:

没有答案