Smart Lock برای رمزهای عبور ، که در سال 2022 منسوخ شده بود، اکنون از Google Play Services Auth SDK ( com.google.android.gms:play-services-auth
) حذف شده است. برای به حداقل رساندن تغییرات شکستهای که ممکن است بر ادغامهای موجود تأثیر بگذارد، قابلیتهای Smart Lock برای همه برنامههای موجود در فروشگاه Play به درستی کار میکنند. نسخههای برنامه جدید که با SDK بهروز شده ( com.google.android.gms:play-services-auth:21.0.0
) وارد شدهاند، دیگر قادر به دسترسی به Smart Lock for Password API نیستند و با موفقیت ساخته نمیشوند. همه توسعه دهندگان باید پروژه های Android خود را برای استفاده از Credential Manager در اسرع وقت مهاجرت کنند.
مزایای مهاجرت به Credential Manager API
Credential Manager یک API ساده و یکپارچه ارائه می دهد که پشتیبانی از ویژگی ها و شیوه های مدرن را در عین بهبود تجربه احراز هویت برای کاربران شما امکان پذیر می کند:
- Credential Manager به طور کامل از ذخیره رمز عبور و احراز هویت پشتیبانی می کند . این به معنای عدم وقفه برای کاربران شما است زیرا برنامه شما از Smart Lock به Credential Manager منتقل می شود.
- Credential Manager پشتیبانی از روشهای ورود به سیستم چندگانه، از جمله کلیدهای عبور و روشهای ورود به سیستم فدرال مانند Sign in with Google را ادغام میکند تا امنیت را افزایش دهد و اگر قصد دارید در آینده از هر کدام پشتیبانی کنید، تبدیل را فعال میکند.
- با شروع Android 14، Credential Manager از ارائه دهندگان رمز عبور و رمز عبور شخص ثالث پشتیبانی می کند.
- Credential Manager یک تجربه کاربری منسجم و یکپارچه را در همه روشهای احراز هویت ارائه میکند.
گزینه های مهاجرت :
مورد استفاده | توصیه |
---|---|
رمز عبور را ذخیره کنید و با رمز عبور ذخیره شده وارد شوید | از گزینه رمز عبور از راهنمای Sign in your user with Credential Manager استفاده کنید. مراحل دقیق برای ذخیره رمز عبور و احراز هویت در ادامه توضیح داده شده است. |
با گوگل وارد شوید | راهنمای Integrate Credential Manager with Sign in with Google را دنبال کنید. |
نمایش شماره تلفن به کاربران | از Phone Number Hint API از Google Identity Services استفاده کنید. |
با استفاده از Credential Manager با رمزهای عبور وارد شوید
برای استفاده از Credential Manager API، مراحل ذکر شده در بخش پیش نیازهای راهنمای Credential Manager را کامل کنید و مطمئن شوید که موارد زیر را انجام می دهید:
- وابستگی های مورد نیاز را اضافه کنید
- کلاس ها را در فایل ProGuard حفظ کنید
- افزودن پشتیبانی برای پیوندهای دارایی دیجیتال (اگر از Smart Lock برای رمزهای عبور استفاده می کنید، این مرحله باید قبلاً اجرا شده باشد)
- مدیریت اعتبارنامه را پیکربندی کنید
- فیلدهای اعتبار را نشان دهید
- رمز عبور کاربر را ذخیره کنید
با رمزهای عبور ذخیره شده وارد شوید
برای بازیابی گزینه های رمز عبور مرتبط با حساب کاربر، این مراحل را کامل کنید:
1. گزینه رمز عبور و احراز هویت را راه اندازی کنید
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
2. از گزینه های بازیابی شده از مرحله قبل برای ساخت درخواست ورود استفاده کنید
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
3. جریان ورود به سیستم را راه اندازی کنید
fun launchSignInFlow() {
coroutineScope.launch {
try {
// Attempt to retrieve the credential from the Credential Manager.
val result = credentialManager.getCredential(
// Use an activity-based context to avoid undefined system UI
// launching behavior.
context = activityContext,
request = getCredRequest
)
// Process the successfully retrieved credential.
handleSignIn(result)
} catch (e: GetCredentialException) {
// Handle any errors that occur during the credential retrieval
// process.
handleFailure(e)
}
}
}
private fun handleSignIn(result: GetCredentialResponse) {
// Extract the credential from the response.
val credential = result.credential
// Determine the type of credential and handle it accordingly.
when (credential) {
is PasswordCredential -> {
val username = credential.id
val password = credential.password
// Use the extracted username and password to perform
// authentication.
}
else -> {
// Handle unrecognized credential types.
Log.e(TAG, "Unexpected type of credential")
}
}
}
private fun handleFailure(e: GetCredentialException) {
// Handle specific credential retrieval errors.
when (e) {
is GetCredentialCancellationException -> {
/* This exception is thrown when the user intentionally cancels
the credential retrieval operation. Update the application's state
accordingly. */
}
is GetCredentialCustomException -> {
/* This exception is thrown when a custom error occurs during the
credential retrieval flow. Refer to the documentation of the
third-party SDK used to create the GetCredentialRequest for
handling this exception. */
}
is GetCredentialInterruptedException -> {
/* This exception is thrown when an interruption occurs during the
credential retrieval flow. Determine whether to retry the
operation or proceed with an alternative authentication method. */
}
is GetCredentialProviderConfigurationException -> {
/* This exception is thrown when there is a mismatch in
configurations for the credential provider. Verify that the
provider dependency is included in the manifest and that the
required system services are enabled. */
}
is GetCredentialUnknownException -> {
/* This exception is thrown when the credential retrieval
operation fails without providing any additional details. Handle
the error appropriately based on the application's context. */
}
is GetCredentialUnsupportedException -> {
/* This exception is thrown when the device does not support the
Credential Manager feature. Inform the user that credential-based
authentication is unavailable and guide them to an alternative
authentication method. */
}
is NoCredentialException -> {
/* This exception is thrown when there are no viable credentials
available for the user. Prompt the user to sign up for an account
or provide an alternative authentication method. Upon successful
authentication, store the login information using
androidx.credentials.CredentialManager.createCredential to
facilitate easier sign-in the next time. */
}
else -> {
// Handle unexpected exceptions.
Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
}
}
}
منابع اضافی
- مرجع نمونه Credential Manager
- Credential Manager codelab
- کاربر خود را با Credential Manager وارد کنید