简体   繁体   中英

Logout in Azure AD B2C Android using MSAL

i have integrated Azure AD B2C on my mobile App using MSAL library . I have created the user flows for SignIn and SignUp . I'am getting the access token once the authentication is success. so i have my logout button designed on my Mobile App on one of my Activity Page, so once i click on my logout button designed on App, i need to get the user signedOut from the App. so how can we do the SignOut in AZURE AD B2C MSAL on Android?

is it ok even if i clear the access token saved on my preference? or is there any other way to signout from Azure side provideb by MSAL library?

The MSAL library provides a logout method that clears the cache in browser storage and sends a sign-out request to Azure Active Directory (Azure AD). Request will be done against the end_session_endpoint URL obtained from the B2C policy metadata . Keep in mind single sign out is supported only by custom policies and that it's scoped to the same browser, not device.

For a Native Android mobile app please use the signout function in Ms Document

To remove this user from the cache, you must call removeAccount() for each policy.

PublicClientApplication publicClient = MyApplication.getInstance().getPublicClient();
User currentUser = Helpers.getUserByPolicy(publicClient.getUsers(), Constant.SIGN_UP_POLICY);
publicClient.remove(currentUser);
//Load account using publicClientApplication 
private fun loadAccounts(mMultipleAccountApp: IMultipleAccountPublicClientApplication) {
        mMultipleAccountApp.getAccounts(object: IPublicClientApplication.LoadAccountsCallback {
            override fun onTaskCompleted(result: List<IAccount>) {
                val accountList: List<IAccount> = result
                removeAccounts(mMultipleAccountApp, accountList)
            }

            override fun onError(exception: MsalException) {
               Log.d("Error",exception.message)
            }
        })
    }


//call remove account
private fun removeAccounts(
        mMultipleAccountApp: IMultipleAccountPublicClientApplication,
        accountList: List<IAccount>
    ) {
        val removeCallback =
            object : IMultipleAccountPublicClientApplication.RemoveAccountCallback {
                override fun onRemoved() {
                    // Redirect to login
                    }

                override fun onError(exception: MsalException) {
                     Log.d("Error",exception.message)
                }
            }

        B2CUser.signOutAsync(
            accountList,
            mMultipleAccountApp, removeCallback
        )

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM