Xác thực bằng Firebase theo cách ẩn danh bằng Unity
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể dùng Firebase Authentication để tạo và sử dụng tài khoản ẩn danh tạm thời
để xác thực với Firebase. Những tài khoản ẩn danh tạm thời này có thể được dùng để
cho phép người dùng chưa đăng ký ứng dụng của bạn làm việc với dữ liệu được bảo vệ
theo các quy tắc bảo mật. Nếu một người dùng ẩn danh quyết định đăng ký ứng dụng của bạn, bạn có thể
liên kết thông tin đăng nhập của họ với
tài khoản để họ có thể tiếp tục làm việc với dữ liệu được bảo vệ của mình trong
các phiên trong tương lai.
Trên trang Phương thức đăng nhập, hãy bật tuỳ chọn Ẩn danh
đăng nhập.
Không bắt buộc: Nếu bạn đã nâng cấp dự án lên
Firebase Authentication with Identity Platform, bạn có thể bật tính năng tự động dọn dẹp. Thời gian
bạn bật chế độ cài đặt này, thì các tài khoản ẩn danh đã tồn tại hơn 30 ngày sẽ tự động
đã bị xoá. Trong các dự án đã bật tính năng tự động dọn dẹp, xác thực ẩn danh sẽ không
được tính vào hạn mức sử dụng hoặc hạn mức thanh toán. Xem
Tự động dọn dẹp.
Xác thực bằng Firebase dưới dạng ẩn danh
Khi người dùng đã đăng xuất sử dụng một tính năng của ứng dụng cần phải xác thực bằng
Firebase, hãy đăng nhập ẩn danh người dùng bằng cách hoàn tất các bước sau:
Lớp FirebaseAuth là cổng vào cho tất cả lệnh gọi API.
Bạn có thể truy cập tệp này thông qua FirebaseAuth.DefaultInstance.
1. Gọi Firebase.Auth.FirebaseAuth.SignInAnonymouslyAsync.
auth.SignInAnonymouslyAsync().ContinueWith(task=>{if(task.IsCanceled){Debug.LogError("SignInAnonymouslyAsync was canceled.");return;}if(task.IsFaulted){Debug.LogError("SignInAnonymouslyAsync encountered an error: "+task.Exception);return;}Firebase.Auth.AuthResultresult=task.Result;Debug.LogFormat("User signed in successfully: {0} ({1})",result.User.DisplayName,result.User.UserId);});
Chuyển đổi tài khoản ẩn danh thành tài khoản vĩnh viễn
Khi người dùng ẩn danh đăng ký ứng dụng của bạn, bạn có thể cho phép họ
tiếp tục công việc của họ với tài khoản mới—ví dụ: bạn có thể muốn
tạo các mặt hàng mà người dùng đã thêm vào giỏ hàng trước khi đăng ký
có sẵn trong giỏ hàng trong tài khoản mới của họ. Để làm như vậy, vui lòng hoàn thành các bước sau
các bước:
Khi người dùng đăng ký, hãy hoàn tất quy trình đăng nhập cho
nhà cung cấp dịch vụ xác thực tối đa, nhưng không bao gồm, gọi một trong
Phương thức Firebase.Auth.FirebaseAuth.SignInAndRetrieveDataWithCredentialAsync. Ví dụ: lấy mã thông báo mã nhận dạng trên Google của người dùng,
Mã truy cập Facebook hoặc địa chỉ email và mật khẩu.
Nhận Firebase.Auth.Credential cho nhà cung cấp dịch vụ xác thực mới:
Truyền đối tượng Firebase.Auth.Credential đến tài khoản của người dùng đăng nhập
Phương thức LinkWithCredentialAsync:
Nếu lệnh gọi đến LinkWithCredentialAsync thành công, tài khoản mới của người dùng có thể
truy cập vào dữ liệu Firebase của tài khoản ẩn danh đó.
Tự động dọn dẹp
Nếu đã nâng cấp dự án lên Firebase Authentication with Identity Platform, bạn có thể
bật tính năng tự động dọn dẹp trong bảng điều khiển Firebase. Khi bật tính năng này, bạn cho phép
Firebase để tự động xoá các tài khoản ẩn danh đã tồn tại hơn 30 ngày. Trong các dự án có chế độ tự động
đã bật tính năng dọn dẹp, xác thực ẩn danh sẽ không bị tính vào giới hạn sử dụng hoặc hạn mức thanh toán.
Mọi tài khoản ẩn danh được tạo sau khi bật tính năng tự động dọn dẹp có thể tự động được
bị xoá bất cứ lúc nào sau 30 ngày sau khi tạo.
Các tài khoản ẩn danh hiện có sẽ đủ điều kiện để tự động bị xoá sau 30 ngày kể từ ngày
bật chế độ tự động dọn dẹp.
Nếu bạn tắt tính năng tự động dọn dẹp, mọi tài khoản ẩn danh được lập lịch bị xoá sẽ vẫn còn
bị lập lịch để xoá.
Nếu bạn "nâng cấp" một tài khoản ẩn danh bằng cách liên kết tài khoản đó với bất kỳ phương thức đăng nhập nào, tài khoản đó sẽ
không bị xoá tự động.
Giờ đây, người dùng có thể xác thực bằng Firebase nên bạn có thể kiểm soát quyền truy cập của họ vào
trong cơ sở dữ liệu Firebase bằng cách sử dụng
Quy tắc Firebase.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-03 UTC."],[],[],null,["You can use Firebase Authentication to create and use temporary anonymous accounts\nto authenticate with Firebase. These temporary anonymous accounts can be used to\nallow users who haven't yet signed up to your app to work with data protected\nby security rules. If an anonymous user decides to sign up to your app, you can\n[link their sign-in credentials to the anonymous\naccount](/docs/auth/unity/account-linking) so that they can continue to work with their protected data in\nfuture sessions.\n\nBefore you begin\n\n1. Before you can use\n [Firebase Authentication](/docs/reference/unity/namespace/firebase/auth),\n you need to add the [Firebase Unity SDK](/download/unity) (specifically,\n `FirebaseAuth.unitypackage`) to your Unity project.\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n **Find detailed instructions for these initial setup steps in\n [Add Firebase to your\n Unity project](/docs/unity/setup#set_up_environment).**\n2. If you haven't yet connected your app to your Firebase project, do so from the [Firebase console](//console.firebase.google.com/).\n3. Enable anonymous auth:\n 1. In the [Firebase console](//console.firebase.google.com/), open the **Auth** section.\n 2. On the **Sign-in Methods** page, enable the **Anonymous** sign-in method.\n 3. **Optional** : If you've upgraded your project to [Firebase Authentication with Identity Platform](/auth#identity-platform), you can enable automatic clean-up. When you enable this setting, anonymous accounts older than 30 days will be automatically deleted. In projects with automatic clean-up enabled, anonymous authentication will no longer count toward usage limits or billing quotas. See [Automatic clean-up](#auto-cleanup).\n\nAuthenticate with Firebase anonymously\n\nWhen a signed-out user uses an app feature that requires authentication with\nFirebase, sign in the user anonymously by completing the following steps:\nThe `FirebaseAuth` class is the gateway for all API calls. It is accessible through [FirebaseAuth.DefaultInstance](/docs/reference/unity/class/firebase/auth/firebase-auth#defaultinstance). \n\n```c#\nFirebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;\n```\n1. Call `Firebase.Auth.FirebaseAuth.SignInAnonymouslyAsync`. \n\n```c#\nauth.SignInAnonymouslyAsync().ContinueWith(task =\u003e {\n if (task.IsCanceled) {\n Debug.LogError(\"SignInAnonymouslyAsync was canceled.\");\n return;\n }\n if (task.IsFaulted) {\n Debug.LogError(\"SignInAnonymouslyAsync encountered an error: \" + task.Exception);\n return;\n }\n\n Firebase.Auth.AuthResult result = task.Result;\n Debug.LogFormat(\"User signed in successfully: {0} ({1})\",\n result.User.DisplayName, result.User.UserId);\n});\n```\n\n\u003cbr /\u003e\n\n| To protect your project from abuse, Firebase limits the number of new email/password and anonymous sign-ups that your application can have from the same IP address in a short period of time. You can request and schedule temporary changes to this quota from the [Firebase console](//console.firebase.google.com/project/_/authentication/providers).\n\nConvert an anonymous account to a permanent account\n\nWhen an anonymous user signs up to your app, you might want to allow them to\ncontinue their work with their new account---for example, you might want to\nmake the items the user added to their shopping cart before they signed up\navailable in their new account's shopping cart. To do so, complete the following\nsteps:\n\n1. When the user signs up, complete the sign-in flow for the user's authentication provider up to, but not including, calling one of the [`Firebase.Auth.FirebaseAuth.SignInAndRetrieveDataWithCredentialAsync`](/docs/reference/unity/class/firebase/auth/firebase-auth#signinandretrievedatawithcredentialasync) methods. For example, get the user's Google ID token, Facebook access token, or email address and password.\n2. Get an `Firebase.Auth.Credential` for the new authentication provider:\n\n3. Pass the `Firebase.Auth.Credential` object to the sign-in user's\n `LinkWithCredentialAsync` method:\n\nIf the call to `LinkWithCredentialAsync` succeeds, the user's new account can\naccess the anonymous account's Firebase data.\n| This technique can also be used to [link any two accounts](/docs/auth/unity/account-linking).\n\nAutomatic clean-up\n\nIf you've upgraded your project to [Firebase Authentication with Identity Platform](/docs/auth#identity-platform), you can\nenable automatic clean-up in the Firebase console. When you enable this feature you allow\nFirebase to automatically delete anonymous accounts older than 30 days. In projects with automatic\nclean-up enabled, anonymous authentication will not count toward usage limits or billing quotas.\n\n- Any anonymous accounts created after enabling automatic clean-up might be automatically deleted any time after 30 days post-creation.\n- Existing anonymous accounts will be eligible for automatic deletion 30 days after enabling automatic clean-up.\n- If you turn automatic clean-up off, any anonymous accounts scheduled to be deleted will remain scheduled to be deleted.\n- If you \"upgrade\" an anonymous account by linking it to any sign-in method, the account will not get automatically deleted.\n\nIf you want to see how many users will be affected before you enable this feature, and you've\nupgraded your project to [Firebase Authentication with Identity Platform](/docs/auth#identity-platform), you can filter by\n`is_anon` in [Cloud\nLogging](https://cloud.google.com/logging/docs).\n\nNext steps\n\nNow that users can authenticate with Firebase, you can control their access to\ndata in your Firebase database using\n[Firebase rules](/docs/database/security#section-authorization)."]]