Anpassa webbläsare och WebViews för iOS/macOS

En webbläsare krävs för interaktiv autentisering. I iOS och macOS 10.15+ använder Microsofts autentiseringsbibliotek (MSAL) systemets webbläsare som standard (som kan visas ovanpå din app) för att göra interaktiv autentisering för att logga in användare. Att använda systemwebbläsaren har fördelen att dela tillståndet för enkel inloggning (SSO) med andra program och med webbprogram.

Du kan ändra upplevelsen genom att anpassa konfigurationen till andra alternativ för att visa webbinnehåll, till exempel:

Endast för iOS:

För iOS och macOS:

MSAL för macOS stöder WKWebView endast äldre os-versioner. ASWebAuthenticationSession stöds endast på macOS 10.15 och senare.

Systemwebbläsare

För iOS, ASWebAuthenticationSession, SFAuthenticationSessionoch SFSafariViewController betraktas som systemwebbläsare. För macOS är endast ASWebAuthenticationSession tillgängligt. I allmänhet delar systemwebbläsare cookies och andra webbplatsdata med Safari-webbläsarprogrammet.

Som standard identifierar MSAL dynamiskt iOS-versionen och väljer den rekommenderade systemwebbläsaren som är tillgänglig för den versionen. På iOS 12+ blir det ASWebAuthenticationSession.

Standardkonfiguration för iOS

Version Webbläsare
iOS 12+ ASWebAuthenticationSession
iOS 11 SFAuthenticationSession
iOS 10 SFSafariViewController

Standardkonfiguration för macOS

Version Webbläsare
macOS 10.15+ ASWebAuthenticationSession
andra versioner WKWebView

Utvecklare kan också välja en annan systemwebbläsare för MSAL-appar:

  • SFAuthenticationSession är iOS 11-versionen av ASWebAuthenticationSession.
  • SFSafariViewController är mer generell och tillhandahåller ett gränssnitt för att surfa på webben och kan även användas för inloggning. I iOS 9 och 10 delas cookies och andra webbplatsdata med Safari – men inte i iOS 11 och senare.

Webbläsare i appen

WKWebView är en webbläsare i appen som visar webbinnehåll. Den delar inte cookies eller webbplatsdata med andra WKWebView-instanser eller med Webbläsaren Safari. WKWebView är en plattformsoberoende webbläsare som är tillgänglig för både iOS och macOS.

Webbläsaren du använder påverkar SSO-upplevelsen på grund av hur de delar cookies. Följande tabeller sammanfattar SSO-funktionerna per webbläsare.

Teknologi Webbläsartyp iOS-tillgänglighet macOS-tillgänglighet Delar cookies och andra data MSAL-tillgänglighet Single Sign-On (SSO)
ASWebAuthenticationSession System iOS12 och uppåt macOS 10.15 och uppåt Yes iOS och macOS 10.15+ med Safari-instanser
SFAuthenticationSession System iOS11 och uppåt N/A Yes endast för iOS med Safari-instanser
SFSafariViewController System iOS11 och uppåt N/A No Endast för iOS Nej**
SFSafariViewController System iOS10 N/A Yes Endast för iOS med Safari-instanser
WKWebView I appen iOS8 och uppåt macOS 10.10 och uppåt No iOS och macOS Nej**

** För att enkel inloggning ska fungera måste token delas mellan appar. Detta kräver en tokencache eller ett koordinatorprogram, till exempel Microsoft Authenticator för iOS.

Ändra standardwebbläsaren för begäran

Du kan använda en webbläsare i appen eller en specifik systemwebbläsare beroende på dina UX-krav genom att ändra följande egenskap i MSALWebviewParameters:

@property (nonatomic) MSALWebviewType webviewType;

Ändra enligt interaktiv begäran

Varje begäran kan konfigureras för att åsidosätta standardwebbläsaren MSALInteractiveTokenParameters.webviewParameters.webviewType genom att ändra egenskapen innan den skickas till API:et acquireTokenWithParameters:completionBlock: .

Dessutom har MSAL stöd för att skicka in en anpassad WKWebView genom att ange egenskapen MSALInteractiveTokenParameters.webviewParameters.customWebView .

Ett exempel:

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)

Om du använder en anpassad webbvy används meddelanden för att ange status för webbinnehållet som visas, till exempel:

/*! 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;

Options

Alla webbläsartyper som stöds av MSAL deklareras i MSALWebviewType-uppräkningen

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,
};

Nästa steg

Läs mer om autentiseringsflöden och programscenarier