【问题标题】:Azure Authentication on Xamarin FormsXamarin 表单上的 Azure 身份验证
【发布时间】:2018-06-21 14:36:37
【问题描述】:

我有一个使用 azure Active Directory 身份验证的 Xamarin Forms 应用程序。
我正在使用 NuGet 数据包 Microsoft.Azure.Mobile.Client 版本 3.1.0,MobileServiceClient 类的 LoginAsync 函数的签名是

Task<MobileServiceUser> LoginAsync(UIViewController view, MobileServiceAuthenticationProvider provider);  

适用于 iOS 和适用于 Android 的类似签名。因此,我在 iOS 上的登录功能是这样的:

var window = UIKit.UIApplication.SharedApplication.KeyWindow;
var current = window.RootViewController;
while(current.PresentedViewController != null) {
    current = current.PresentedViewController;
}
var user = await MSC.LoginAsync(current, MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory);
return user;

使用此函数,库会将应用程序发送到 Azure Active Directory 网页以进行登录或注册,并返回一个 MobileServiceUser 实例。

该版本按预期工作。

但是,由于应用程序其他部分的一些问题,我不得不将 NuGet 数据包更新到较新的版本,在本例中为 4.0.2。登录功能将签名更改为

Task<MobileServiceUser> LoginAsync(MobileServiceAuthenticationProvider provider, JObject token);  

我不知道如何处理最后一个参数才能获得与以前相同的行为。传递null 会抛出异常,传递新的JObject 类不起作用。

【问题讨论】:

    标签: azure xamarin.forms


    【解决方案1】:

    var user = await MSC.LoginAsync(current, MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory);

    根据您在 Microsoft.Azure.Mobile.Client v3.1.0 下为登录传递的参数,您使用的是Server-managed authentication,这意味着您的移动后端将帮助您联系相关的身份提供者并直接接收令牌,然后生成一个authenticationToken,并返回给移动客户端进行后续授权请求。

    作为Azure Mobile Apps Managed SDK Change Log 关于版本 4.0.0

    • .NET Standard 1.4 支持
    • [Xamarin.Android] 支持服务器登录流程以在 Android 上使用 Chrome 自定义选项卡。它支持 OAuth 2.0 PKCE 扩展。
    • [Xamarin.iOS] 支持服务器登录流程以在 iOS 上使用 SafariViewController。它支持 OAuth 2.0 PKCE 扩展。
    • [UWP] 支持服务器登录流程以在 Windows 上使用浏览器。它支持 OAuth 2.0 PKCE 扩展。

    为了在4.0.0+版本下使用相同的认证流程,需要使用如下方法重载:

    var user = await TodoItemManager.DefaultManager.CurrentClient
                 .LoginAsync(MobileServiceAuthenticationProvider.Facebook, "{url_scheme_of_your_app}");
    

    您可以按照here 在 Azure 门户上为您的应用程序定义 URL 方案。详细步骤可以关注Add authentication to your Xamarin Forms app

    Task LoginAsync(MobileServiceAuthenticationProvider provider, JObject token);

    对于上述LoginAsync的方法重载会使用Client-managed authentication,对于这种方法,您需要直接联系您的身份提供者以获取令牌,然后您需要将令牌发送到您的移动后端进行交换authenticationToken

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2018-07-30
      • 2017-12-15
      • 2017-12-15
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多