自定义适用于 iOS/macOS 的浏览器和 WebView

完成交互式身份验证需要使用 Web 浏览器。 在 iOS 和 macOS 10.15+ 上,Microsoft 身份验证库(MSAL)默认使用系统 Web 浏览器(可能显示在应用顶部),以执行交互式身份验证以登录用户。 使用系统浏览器可以与其他应用程序和 Web 应用程序共享单一登录(SSO)状态。

可以通过将配置自定义为显示 Web 内容的其他选项来更改体验,例如:

仅适用于 iOS:

对于 iOS 和 macOS:

macOS 版 MSAL 仅在较旧的操作系统版本上支持 WKWebViewASWebAuthenticationSession 仅在 macOS 10.15 及更高版本上受支持。

系统浏览器

对于 iOS、ASWebAuthenticationSessionSFAuthenticationSessionSFSafariViewController被视为系统浏览器。 对于 macOS,仅 ASWebAuthenticationSession 可用。 通常,系统浏览器与 Safari 浏览器应用程序共享 Cookie 和其他网站数据。

默认情况下,MSAL 将动态检测 iOS 版本,并选择该版本上可用的推荐系统浏览器。 在 iOS 12+ 上,它将是 ASWebAuthenticationSession

iOS 的默认配置

版本 web 浏览器
iOS 12+ ASWebAuthenticationSession
iOS 11 SFAuthenticationSession
iOS 10 SFSafariViewController

macOS 的默认配置

版本 web 浏览器
macOS 10.15+ ASWebAuthenticationSession
其他版本 WKWebView

开发人员还可以为 MSAL 应用选择其他系统浏览器:

  • SFAuthenticationSession 是 iOS 11 版本的 ASWebAuthenticationSession
  • SFSafariViewController 更常规用途,并提供用于浏览 Web 的界面,也可用于登录目的。 在 iOS 9 和 10 中,Cookie 和其他网站数据与 Safari 共享-但在 iOS 11 及更高版本中不共享。

应用内浏览器

WKWebView 是显示 Web 内容的应用内浏览器。 它不会与其他 WKWebView 实例或 Safari 浏览器共享 Cookie 或网站数据。 WKWebView 是适用于 iOS 和 macOS 的跨平台浏览器。

使用的浏览器会影响 SSO 体验,因为它们共享 Cookie 的方式。 下表汇总了每个浏览器的 SSO 体验。

科技 浏览器类型 iOS 可用性 macOS 可用性 共享 Cookie 和其他数据 MSAL 可用性 SSO
ASWebAuthenticationSession System iOS 12 及更高版本 macOS 10.15 及更新 是的 iOS 和 macOS 10.15+ 使用 Safari 实例
SFAuthenticationSession System iOS 11 及以上版本 N/A 是的 仅限 iOS 使用 Safari 实例
SFSafariViewController System iOS 11 及以上版本 N/A No 仅限 iOS 否​**
SFSafariViewController System iOS10 N/A 是的 仅限 iOS 使用 Safari 实例
WKWebView 应用内 iOS 8 及以上版本 macOS 10.10 及更新 No iOS 和 macOS 否​**

** 若要运行 SSO,需要在应用之间共享令牌。 这需要令牌缓存或中转站应用程序,例如适用于 iOS 的 Microsoft Authenticator。

更改请求的默认浏览器

你可以根据 UX 要求,通过在 MSALWebviewParameters 中更改以下属性,使用应用内浏览器或特定的系统浏览器:

@property (nonatomic) MSALWebviewType webviewType;

每次交互请求的更改

可以将每个请求配置为在将属性传递给 MSALInteractiveTokenParameters.webviewParameters.webviewType API 之前更改acquireTokenWithParameters:completionBlock:属性来替代默认浏览器。

此外,MSAL 还支持通过设置 MSALInteractiveTokenParameters.webviewParameters.customWebView 属性来传入自定义的 WKWebView

例如:

Objective-C

UIViewController *myParentController = ...;
WKWebView *myCustomWebView = ...;
MSALWebviewParameters *webViewParameters = [[MSALWebviewParameters alloc] initWithAuthPresentationViewController:myParentController];
webViewParameters.webviewType = MSALWebviewTypeWKWebView;
webViewParameters.customWebview = myCustomWebView;
MSALInteractiveTokenParameters *interactiveParameters = [[MSALInteractiveTokenParameters alloc] initWithScopes:@[@"myscope"] webviewParameters:webViewParameters];

[app acquireTokenWithParameters:interactiveParameters completionBlock:completionBlock];

Swift

let myParentController: UIViewController = ...
let myCustomWebView: WKWebView = ...
let webViewParameters = MSALWebviewParameters(authPresentationViewController: myParentController)
webViewParameters.webviewType = MSALWebviewType.wkWebView
webViewParameters.customWebview = myCustomWebView
let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["myscope"], webviewParameters: webViewParameters)

app.acquireToken(with: interactiveParameters, completionBlock: completionBlock)

如果使用自定义 Web 视图,则通知用于指示正在显示的 Web 内容的状态,例如:

/*! Fired at the start of a resource load in the webview. The URL of the load, if available, will be in the @"url" key in the userInfo dictionary */
extern NSString *MSALWebAuthDidStartLoadNotification;

/*! Fired when a resource finishes loading in the webview. */
extern NSString *MSALWebAuthDidFinishLoadNotification;

/*! Fired when web authentication fails due to reasons originating from the network. Look at the @"error" key in the userInfo dictionary for more details.*/
extern NSString *MSALWebAuthDidFailNotification;

/*! Fired when authentication finishes */
extern NSString *MSALWebAuthDidCompleteNotification;

/*! Fired before ADAL invokes the broker app */
extern NSString *MSALWebAuthWillSwitchToBrokerApp;

选项

所有 MSAL 支持的 Web 浏览器类型都在 MSALWebviewType 枚举中声明了

typedef NS_ENUM(NSInteger, MSALWebviewType)
{
    /**
     For iOS 11 and up, uses AuthenticationSession (ASWebAuthenticationSession or SFAuthenticationSession).
     For older versions, with AuthenticationSession not being available, uses SafariViewController.
     For macOS 10.15 and above uses ASWebAuthenticationSession
     For older macOS versions uses WKWebView
     */
    MSALWebviewTypeDefault,

    /** Use ASWebAuthenticationSession where available.
     On older iOS versions uses SFAuthenticationSession
     Doesn't allow any other webview type, so if either of these are not present, fails the request*/
    MSALWebviewTypeAuthenticationSession,

#if TARGET_OS_IPHONE

    /** Use SFSafariViewController for all versions. */
    MSALWebviewTypeSafariViewController,

#endif
    /** Use WKWebView */
    MSALWebviewTypeWKWebView,
};

后续步骤

详细了解 身份验证流和应用程序方案