【问题标题】:Xamarin Microsoft.Identity.Client AuthenticationResult AccessToken is nullXamarin Microsoft.Identity.Client AuthenticationResult AccessToken 为空
【发布时间】:2017-06-11 17:43:23
【问题描述】:

我正在创建一个使用 Azure B2C 对用户进行身份验证的 Xamarin.Forms PCL 应用程序。我之前使用的是 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 现在正在执行,但它返回 null,所以我仍然无法调用表。

【问题讨论】:

    标签: xamarin xamarin.forms azure-active-directory azure-ad-b2c msal


    【解决方案1】:

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

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

    之后我使用了来自Azure Mobile Client SDKMobileServiceClient。而对于新的MobileServiceClient,我们只需要提供移动应用程序的 URL,如下代码:

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

    但是,如果我使用来自 Azure Mobile Services SDKMobileServiceClient ,我可以用 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();
    

    【讨论】:

    • 您是如何登录到 mobileClient 的?我正在使用从 Microsoft.Identity.Client PublicClientApplication.LoginAsync() 返回的 AuthenticationResult,然后使用 AuthenticaitonResult 中返回的 IdToken 将“access_token”添加到 JObject。有了这些更改,我仍然没有获得授权。
    • 我只是使用 LoginAsync 登录 mobileClient 并提供您提到的 id_token。你用的是哪个库?我建议您使用 Fiddler 捕获请求以检查端点是否正确,因为正如我在帖子中提到的那样,不同的库是不同的。它将错过请求路径中的.auth,如https://{HostURL}/login/aad。并且我也更新了测试代码示例,如果你还有问题,也请分享 Azure 上移动应用的设置。
    • 我正在使用 Microsoft.WindowsAzure.MobileServices 并且我确实拥有 .auth
    • 我将快速添加我的设置并显示我当前的调用方式
    • 我完成了 LoginAsync 方法,我将错误的 App Id 添加到了我的允许令牌受众。但是,LoginAsync 返回一个 null MobileServiceUser,所以还是有问题。
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多