自訂 iOS/macOS 的瀏覽器和 WebView

互動式驗證需要網頁瀏覽器。 在 iOS 和 macOS 10.15+ 上,Microsoft 驗證資源庫(MSAL)預設使用系統瀏覽器(可能會顯示在應用程式頂端)來進行互動式認證以登入使用者。 使用系統瀏覽器的優點是與其他應用程式及網頁應用程式共享單一登入(SSO)狀態。

你可以自訂設定,加入其他顯示網頁內容的選項,例如:

僅限 iOS 版本:

適用於 iOS 與 macOS:

macOS 版 MSAL 僅支援 WKWebView 較舊的作業系統版本。 ASWebAuthenticationSession 僅支援 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 較為通用,提供網頁瀏覽介面,也可用於登入。 在 iOS 9 和 10 中,Cookie 和其他網站資料會與 Safari 共享——但在 iOS 11 及以後版本則不然。

應用程式內瀏覽器

WKWebView 是一款應用程式內瀏覽器,可顯示網頁內容。 它不會與其他 WKWebView 實例或 Safari 瀏覽器共享 Cookie 或網站資料。 WKWebView 是一款跨平台瀏覽器,支援 iOS 與 macOS。

你使用的瀏覽器會影響 SSO 體驗,因為它們分享 Cookie 的方式。 以下表格彙整了各瀏覽器中的 SSO(單一登入)體驗。

科技 瀏覽器類型 iOS 可用性 macOS 可用性 分享 Cookie 及其他資料 MSAL 可用性 SSO
ASWebAuthenticationSession System iOS12 及以上版本 macOS 10.15 及以上版本 Yes iOS 與 macOS 10.15+ 附帶 Safari 實例
SFAuthenticationSession System iOS11 及以上 N/A Yes 僅限 iOS 附帶 Safari 實例
SFSafariView控制器 System iOS11 及以上 N/A No 僅限 iOS 否**
SFSafariView控制器 System iOS10 N/A Yes 僅限 iOS 附帶 Safari 實例
WKWebView 應用程式內 iOS8 及以上版本 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)

如果您使用自訂網頁檢視,通知會用來顯示所顯示的網頁內容狀態,例如:

/*! 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 的網頁瀏覽器類型皆在 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,
};

下一步

了解更多關於認證流程與應用情境的資訊