共用方式為


教學指南:在 iOS/macOS 應用程式中,使用者註冊後自動登入

適用於帶有白色核取記號符號的綠色圓圈,表示下列內容適用於外部租用戶。 外部租用戶 (深入瞭解

本教學課程示範如何在使用原生驗證註冊 iOS/macOS 應用程式之後自動登入使用者。

在本教學課程中,您會:

  • 註冊完成後,再進行登入。
  • 處理錯誤。

先決條件

註冊完畢後登入

Sign in after sign up 是一個登入用戶流程的增強功能,其效果是在成功註冊後自動登入。 SDK 可讓開發人員在註冊之後登入使用者,而不需要提供使用者名稱,或透過一次性密碼驗證電子郵件位址。

若要在成功註冊之後登入使用者,請從 signIn(delegate)傳回的新狀態 SignInAfterSignUpState 中,使用 onSignUpCompleted(newState) 方法:

extension ViewController: SignUpVerifyCodeDelegate {
    func onSignUpVerifyCodeError(error: MSAL.VerifyCodeError, newState: MSAL.SignUpCodeRequiredState?) {
        resultTextView.text = "Error verifying code: \(error.errorDescription ?? "no description")"
    }

    func onSignUpCompleted(newState: SignInAfterSignUpState) {
        resultTextView.text = "Signed up successfully!"
        let parameters = MSALNativeAuthSignInAfterSignUpParameters()
        newState.signIn(parameters: parameters, delegate: self)
    }
}

signIn(parameters:delegate) 接受 MSALNativeAuthSignInAfterSignUpParameters 實例和委派參數,我們必須在 SignInAfterSignUpDelegate 通訊協定中實作必要的方法。

在最常見的案例中,我們會收到呼叫 onSignInCompleted(result),指出使用者已登入。 結果可用來擷取 access token

extension ViewController: SignInAfterSignUpDelegate {
    func onSignInAfterSignUpError(error: SignInAfterSignUpError) {
        resultTextView.text = "Error signing in after sign up"
    }

    func onSignInCompleted(result: MSAL.MSALNativeAuthUserAccountResult) {
        // User successfully signed in
        let parameters = MSALNativeAuthGetAccessTokenParameters()
        result.getAccessToken(parameters: parameters, delegate: self)
    }
}

getAccessToken(parameters:delegate) 接受 MSALNativeAuthGetAccessTokenParameters 實例和委派參數,我們必須在 CredentialsDelegate 通訊協定中實作必要的方法。

在最常見的情況下,我們接到來電 onAccessTokenRetrieveCompleted(result),表示使用者已取得 access token

extension ViewController: CredentialsDelegate {
    func onAccessTokenRetrieveError(error: MSAL.RetrieveAccessTokenError) {
        resultTextView.text = "Error retrieving access token"
    }

    func onAccessTokenRetrieveCompleted(result: MSALNativeAuthTokenResult) {
        resultTextView.text = "Signed in. Access Token: \(result.accessToken)"
    }
}

設定自定義宣告提供者

如果您想要將外部系統的宣告新增至發行給應用程式的權杖中,請使用 自訂宣告提供者。 自定義宣告提供者是由自定義驗證延伸模組所組成,可呼叫外部 REST API 從外部系統擷取宣告。

請遵循 設定自定義宣告提供者 中的步驟,將外部系統的宣告新增至安全性令牌。

下一步