首页
/ 在Xamarin Forms应用中集成Microsoft身份验证、Microsoft Authenticator应用(Broker)和Microsoft Graph

在Xamarin Forms应用中集成Microsoft身份验证、Microsoft Authenticator应用(Broker)和Microsoft Graph

2025-06-19 13:53:46作者:羿妍玫Ivan

项目概述

本文将详细介绍如何在Xamarin Forms移动应用中实现以下功能:

  1. 通过Microsoft Authenticator应用(Broker)对工作/学校账户(AAD)和个人Microsoft账户(MSA)进行身份验证
  2. 通过Broker获取访问令牌来调用Microsoft Graph API

技术架构

该解决方案基于Xamarin.Forms框架,支持iOS和Android平台(不包含UWP),使用MSAL.NET(Microsoft身份验证库)实现身份验证流程。

架构图

Broker机制解析

Broker(代理)是移动设备上的一种特殊应用(如Microsoft Authenticator),它在身份验证流程中扮演关键角色:

Broker的核心优势

  • 单点登录(SSO):用户无需为每个应用单独登录
  • 设备身份验证:通过访问设备证书验证设备身份
  • 应用验证:Broker会验证调用它的应用的合法性

平台支持情况

  • iOS和Android:可使用Microsoft Authenticator应用
  • Android:还可使用Intune Company Portal作为Broker

环境准备

开发工具要求

  • Visual Studio 2019(需安装"Mobile development with .NET"工作负载)
  • Xamarin.iOS for Visual Studio(如需运行iOS应用)
  • 互联网连接

账户要求

  • Microsoft个人账户(MSA)
  • 或Azure AD工作/学校账户

实现步骤详解

1. 应用注册配置

在Azure门户中完成以下配置:

  1. 创建新应用注册
  2. 设置应用名称(如"active-directory-xamarin-native-v2")
  3. 支持的账户类型选择"任何组织目录中的账户和个人Microsoft账户"
  4. 配置重定向URI:
    • 格式:msal<clientId>://auth
  5. 添加API权限:Microsoft Graph的User.Read权限

2. 项目配置

通用配置

  1. 在App.cs中设置ClientID为注册应用的应用程序ID
  2. 配置Broker支持(代码已内置):
var app = PublicClientApplicationBuilder
            .Create(ClientId)
            .WithBroker()
            .WithReplyUri(mobileRedirectURI) 
            .Build();

iOS特有配置

  1. 在AppDelegate.cs中配置OpenUrl方法处理Broker回调
  2. 设置RootViewController:
App.RootViewController = new UIViewController();
  1. 在Info.plist中配置URL Scheme:
<key>CFBundleURLSchemes</key>
<array>
  <string>msauth[APPLICATIONID]</string>
</array>
  1. 添加LSApplicationQueriesSchemes:
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>msauthv2</string>
  <string>msauthv3</string>
</array>

Android特有配置

  1. 在MainActivity.cs中重写OnActivityResult方法
  2. 配置MsalActivity:
[Activity]
[IntentFilter(new[] { Intent.ActionView },
      Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
      DataHost = "auth",
      DataScheme = "msal[ClientID]")]
public class MsalActivity : BrowserTabActivity
{
}

3. 重定向URI配置

iOS重定向URI

格式:msauth.{BundleId}://auth

示例:msauth.com.yourcompany.XForms://auth

Android重定向URI

格式:msauth://{Package Name}/{Signature Hash}

获取签名哈希的方法:

Windows:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64

Mac:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

4. 系统浏览器备用方案(Android)

当设备未安装Broker时,可配置系统浏览器作为备用方案:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="msalClientID" android:host="auth" />
  <data android:scheme="msauth"
              android:host="package name"
              android:path="/Package Signature"/>
</intent-filter>

最佳实践建议

  1. 错误处理:妥善处理MSAL抛出的异常,特别是当Broker不可用时
  2. 测试策略
    • 测试Broker安装和未安装两种情况
    • 测试单账户和多账户场景
  3. 性能优化:考虑实现令牌缓存机制减少身份验证频率
  4. 用户体验
    • 提供清晰的登录状态指示
    • 处理网络不可用情况

常见问题排查

  1. iOS Broker调用失败

    • 确认RootViewController已正确设置
    • 检查Info.plist中的URL Scheme配置
  2. Android重定向URI不匹配

    • 确保签名哈希计算正确
    • 验证包名拼写无误
  3. Broker响应未处理

    • 确认AppDelegate.cs(iOS)或MainActivity.cs(Android)中的回调处理代码已正确实现

通过以上步骤,开发者可以在Xamarin Forms应用中实现基于Broker的高级身份验证方案,提供更安全、更便捷的用户体验。

登录后查看全文
热门项目推荐