احراز هویت با استفاده از SAML در برنامه های وب
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
اگر به Firebase Authentication with Identity Platform ارتقا داده اید، می توانید با استفاده از ارائه دهنده هویت SAML انتخابی، کاربران خود را با Firebase احراز هویت کنید. این امکان استفاده از راه حل SSO مبتنی بر SAML را برای ورود کاربران به برنامه Firebase شما فراهم می کند.
Firebase Authentication فقط از جریان SAML آغاز شده توسط ارائه دهنده خدمات پشتیبانی می کند.
قبل از شروع
برای ورود به سیستم کاربران با استفاده از ارائه دهنده هویت SAML، ابتدا باید اطلاعاتی را از ارائه دهنده جمع آوری کنید:
- شناسه نهاد ارائه دهنده : یک URI که ارائه دهنده هویت را شناسایی می کند.
- نشانی اینترنتی SAML SSO ارائهدهنده : نشانی اینترنتی صفحه ورود به سیستم ارائهدهنده هویت.
- گواهی کلید عمومی ارائه دهنده : گواهی مورد استفاده برای اعتبارسنجی نشانه های امضا شده توسط ارائه دهنده هویت.
- شناسه نهاد برنامه شما : یک URI که برنامه شما، «ارائهدهنده خدمات» را شناسایی میکند.
پس از دریافت اطلاعات فوق، SAML را به عنوان ارائهدهنده ورود به سیستم برای پروژه Firebase خود فعال کنید:
Firebase را به پروژه جاوا اسکریپت خود اضافه کنید .
اگر Firebase Authentication with Identity Platform ارتقا ندادهاید، این کار را انجام دهید. احراز هویت SAML فقط در پروژه های ارتقا یافته در دسترس است.
در صفحه ارائه دهندگان ورود به سیستم کنسول Firebase ، روی افزودن ارائه دهنده جدید کلیک کنید و سپس روی SAML کلیک کنید.
یک نام به این ارائه دهنده بدهید. به شناسه ارائه دهنده تولید شده توجه کنید: چیزی شبیه saml.example-provider
. وقتی کد ورود به برنامه را اضافه میکنید، به این شناسه نیاز دارید.
شناسه نهاد، نشانی وب SSO و گواهی کلید عمومی ارائه دهنده هویت خود را مشخص کنید. همچنین شناسه نهاد برنامه خود (ارائه دهنده خدمات) را مشخص کنید. این مقادیر باید دقیقاً با مقادیری مطابقت داشته باشند که ارائه دهنده شما به شما اختصاص داده است.
تغییرات خود را ذخیره کنید
اگر قبلاً دامنه برنامه خود را تأیید نکردهاید، آن را به فهرست مجاز در صفحه تأیید هویت > تنظیمات کنسول Firebase اضافه کنید.
جریان ورود به سیستم را با Firebase SDK مدیریت کنید
برای مدیریت جریان ورود به سیستم با Firebase JavaScript SDK، این مراحل را دنبال کنید:
با استفاده از شناسه ارائه دهنده ای که در کنسول Firebase دریافت کرده اید، نمونه ای از SAMLAuthProvider
ایجاد کنید.
Web
import { SAMLAuthProvider } from "firebase/auth";
const provider = new SAMLAuthProvider('saml.example-provider');
Web
var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider');
``
با استفاده از شی ارائه دهنده SAML با Firebase احراز هویت کنید.
شما می توانید کاربر را به صفحه ورود به سیستم ارائه دهنده هدایت کنید یا صفحه ورود به سیستم را در یک پنجره بازشو مرورگر باز کنید.
تغییر مسیر جریان
با فراخوانی signInWithRedirect()
به صفحه ورود به سیستم ارائه دهنده هدایت شوید:
Web
import { getAuth, signInWithRedirect } from "firebase/auth";
const auth = getAuth();
signInWithRedirect(auth, provider);
Web
firebase.auth().signInWithRedirect(provider);
پس از اینکه کاربر ورود به سیستم را کامل کرد و به برنامه شما بازگشت، میتوانید با فراخوانی getRedirectResult()
نتیجه ورود به سیستم را دریافت کنید.
Web
import { getAuth, getRedirectResult, SAMLAuthProvider } from "firebase/auth";
const auth = getAuth();
getRedirectResult(auth)
.then((result) => {
// User is signed in.
// Provider data available using getAdditionalUserInfo()
})
.catch((error) => {
// Handle error.
});
Web
firebase.auth().getRedirectResult()
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
جریان پاپ آپ
Web
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
Web
firebase.auth().signInWithPopup(provider)
.then((result) => {
// User is signed in.
// Provider data available in result.additionalUserInfo.profile,
// or from the user's ID token obtained from result.user.getIdToken()
// as an object in the firebase.sign_in_attributes custom claim.
})
.catch((error) => {
// Handle error.
});
نشانه ID و شی UserInfo فقط در صورتی حاوی آدرس ایمیل کاربر است که در ویژگی NameID
ادعای SAML از ارائه دهنده هویت ارائه شده باشد:
<Subject>
<NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">test@email.com</NameID>
</Subject>
در حالی که مثالهای بالا بر جریانهای ورود به سیستم تمرکز دارند، میتوانید از همان الگو برای پیوند دادن یک ارائهدهنده SAML به یک کاربر موجود با استفاده از linkWithRedirect()
و linkWithPopup()
استفاده کنید و یک کاربر را با reauthenticateWithRedirect()
و reauthenticateWithPopup()
احراز هویت مجدد کنید. که می تواند برای بازیابی اعتبارنامه های جدید برای عملیات حساسی که نیاز به ورود اخیر دارند استفاده شود.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-08 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-08 بهوقت ساعت هماهنگ جهانی."],[],[],null,["If you've upgraded to Firebase Authentication with Identity Platform, you can authenticate your users with Firebase\nusing the SAML identity provider of your choice. This makes it possible to use\nyour SAML-based SSO solution to sign users in to your Firebase app.\n\nFirebase Authentication supports only the service-provider initiated SAML flow.\n\nBefore you begin\n\nTo sign in users using a SAML identity provider, you must first collect some\ninformation from the provider:\n\n- **The provider's Entity ID**: A URI that identifies the identity provider.\n- **The provider's SAML SSO URL**: The URL of the identity provider's sign-in page.\n- **The provider's public key certificate**: The certificate used to validate tokens signed by the identity provider.\n- **Your app's Entity ID**: A URI that identifies your app, the \"service provider\".\n\nAfter you have the above information, enable SAML as a sign-in provider for your\nFirebase project:\n\n1. [Add Firebase to your JavaScript project](/docs/web/setup).\n\n2. If you haven't upgraded to Firebase Authentication with Identity Platform, do so. SAML authentication is only\n available in upgraded projects.\n\n3. On the [**Sign-in providers**](//console.firebase.google.com/project/_/authentication/providers)\n page of the Firebase console, click **Add new provider** , and then click\n **SAML**.\n\n4. Give a name to this provider. Note the provider ID that's generated:\n something like `saml.example-provider`. You'll need this ID when you add\n sign-in code to your app.\n\n5. Specify your identity provider's entity ID, SSO URL, and public key\n certificate. Also specify the entity ID of your app (the service provider).\n These values must exactly match the values your provider assigned to you.\n\n6. Save your changes.\n\n7. If you haven't already authorized your app's domain, add it to the allow\n list on the [**Authentication \\\u003e Settings**](//console.firebase.google.com/project/_/authentication/settings)\n page of the Firebase console.\n\nHandle the sign-in flow with the Firebase SDK\n\nTo handle the sign-in flow with the Firebase JavaScript SDK, follow these\nsteps:\n\n1. Create an instance of an `SAMLAuthProvider` using the provider ID you got in\n the Firebase console.\n\n Web \n\n import { SAMLAuthProvider } from \"firebase/auth\";\n\n const provider = new SAMLAuthProvider('saml.example-provider');\n\n Web \n\n var provider = new firebase.auth.SAMLAuthProvider('saml.example-provider');\n ``\n\n\u003c!-- --\u003e\n\n1. Authenticate with Firebase using the SAML provider object.\n\n You can either redirect the user to the provider's sign-in page or open the\n sign-in page in a pop-up browser window.\n\n **Redirect flow**\n\n Redirect to the provider sign-in page by calling `signInWithRedirect()`: \n\n Web \n\n import { getAuth, signInWithRedirect } from \"firebase/auth\";\n\n const auth = getAuth();\n signInWithRedirect(auth, provider);\n\n Web \n\n firebase.auth().signInWithRedirect(provider);\n\n After the user completes sign-in and returns to your app, you can obtain the\n sign-in result by calling `getRedirectResult()`. \n\n Web \n\n import { getAuth, getRedirectResult, SAMLAuthProvider } from \"firebase/auth\";\n\n const auth = getAuth();\n getRedirectResult(auth)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available using getAdditionalUserInfo()\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n Web \n\n firebase.auth().getRedirectResult()\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n **Pop-up flow**\n\n Web \n\n import { getAuth, signInWithPopup, OAuthProvider } from \"firebase/auth\";\n\n const auth = getAuth();\n signInWithPopup(auth, provider)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n Web \n\n firebase.auth().signInWithPopup(provider)\n .then((result) =\u003e {\n // User is signed in.\n\n // Provider data available in result.additionalUserInfo.profile,\n // or from the user's ID token obtained from result.user.getIdToken()\n // as an object in the firebase.sign_in_attributes custom claim.\n })\n .catch((error) =\u003e {\n // Handle error.\n });\n\n The ID token and [UserInfo](/docs/reference/js/auth.userinfo#userinfoemail)\n object contains the user's email address only if it is provided in the\n `NameID` attribute of the SAML assertion from the identity provider: \n\n \u003cSubject\u003e\n \u003cNameID Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress\"\u003etest@email.com\u003c/NameID\u003e\n \u003c/Subject\u003e\n\n2. While the above examples focus on sign-in flows, you can use the same\n pattern to link a SAML provider to an existing user using\n `linkWithRedirect()` and `linkWithPopup()`, and re-authenticate a user with\n `reauthenticateWithRedirect()` and `reauthenticateWithPopup()`, which can be\n used to retrieve fresh credentials for sensitive operations that require\n recent login."]]