Xamarin Microsoft.Identity.Client AuthenticationResult AccessToken 为空

Xamarin Microsoft.Identity.Client AuthenticationResult AccessToken is null

我正在创建一个 Xamarin.Forms PCL 应用程序,它使用 Azure B2C 对用户进行身份验证。我之前使用的是 Microsoft.Identity.Client 版本 1.0.304142221-alpha,但在 NuGet 上发布后我才更新到 1.1.0-preview。

我还使用 Azure MobileServiceClient 登录用户,因此只有经过身份验证的用户才能调用我的表。

我能够成功进行身份验证,我设置为 this sample on GitHub.

使用以前版本的 Microsoft.Identity.Client,我可以这样登录 MobileServiceClient

AuthenticationResult ar = await App.AuthenticationClient.AcquireTokenAsync(Config.Scopes, 
         string.Empty, UiOptions.SelectAccount, string.Empty, null, 
         Config.Authority, Config.SignUpSignInpolicy);

JObject payload = getPayload(ar.IdToken);
payload["access_token"] = ar.Token;
string objectId = payload["oid"].ToString();

MobileServiceUser u = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,
                      payload);

但是,更新后,AuthenticationResult不再有名为Token的成员。相反,它有 AccessToken,对我来说,它总是返回 null

我曾尝试使用 IdToken 登录 MobileServiceClient,但这会产生未经授权的错误。

我认为这个问题可能与我定义的范围有关。现在我有:

public static string[] Scopes = { "https://<MyTennant>/<MyAPIName>.read"};

我是否缺少任何范围以获取 AccessToken 或者是其他地方的问题?

更新:这是我在 Azure 门户中的设置 对于我的 API:

对于我的本地客户端:

在我的应用程序中,我是这样登录的:

AuthenticationResult ar = await App.AuthenticationClient.AcquireTokenAsync(Scopes, 
                     GetUserByPolicy(App.AuthenticationClient.Users, PolicySignUpSignIn),
                     App.UiParent);
payload = getPayload(ar.IdToken);
payload["access_token"] = ar.IdToken;

var mobileService = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://giftthis.azurewebsites.net/.auth/");
MobileServiceUser u = await mobileService.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);

LoginAsync 正在执行,但它 returns 为空,所以我仍然无法调用表。

我正在尝试使用 Microsoft.Identity.Client 版本 1.1.0-preview 测试这个问题,但它对我来说效果很好。

移动应用程序使用具有 高级 模式的 Azure AD B2C 应用程序进行保护,并使用如图所示的应用程序 ID 设置 ALLOWED TOKEN AUDIENCES以下:

之后我使用了 Azure Mobile Client SDK 中的 MobileServiceClient。而要新建 MobileServiceClient 我们只需要提供移动应用程序 URL 如下代码:

string applicationUrl = "https://mobilefei.azurewebsites.net/";
var mobileClient = new MobileServiceClient(applicationUrl);

但是,如果我使用 Azure Mobile Services SDK 中的 MobileServiceClient ,我可以用 401 错误重现相同的问题。在这种情况下,当我们需要附加 .auth 来初始化 MobileServiceClient 时,如下代码:

string applicationUrl = "https://mobilefei.azurewebsites.net/.auth/";
var mobileClient = new MobileServiceClient(applicationUrl);

更新:

string CLIENT_ID = "420a3a24-97cf-46ca-a882-f6c047b0d845";
string[] SCOPES = { "https://xxx.onmicrosoft.com/b2cwebapp/read" };
string Tenant = "xxx.onmicrosoft.com";

string PolicySignUpSignIn = "B2C_1_Sign_In";
string AuthorityBase = $"https://login.microsoftonline.com/tfp/{Tenant}/";
string Authority = $"{AuthorityBase}{PolicySignUpSignIn}";


PublicClientApplication myApp = new PublicClientApplication(CLIENT_ID, Authority);

AuthenticationResult authenticationResult = myApp.AcquireTokenAsync(SCOPES).Result;

Console.WriteLine($"AccessToken:\n{authenticationResult.AccessToken}");

Console.WriteLine($"IdToken:\n{authenticationResult.IdToken}");

//This applicationUrl  works for WindowsAzure.MobileServices
//string applicationUrl = "https://mobilefei.azurewebsites.net/.auth/";

//This applicationUrl  works for Microsoft.Azure.Mobile.Client
string applicationUrl = "https://mobilefei.azurewebsites.net/";
var mobileClient = new MobileServiceClient(applicationUrl);

JObject token = new JObject();
token.Add("access_token", authenticationResult.IdToken);
var user = mobileClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, token).Result;
Console.WriteLine($"UserID:\n{user.UserId}");
Console.Read();