ตรวจสอบสิทธิ์โดยใช้ Microsoft บนแพลตฟอร์ม Apple

คุณสามารถให้ผู้ใช้ตรวจสอบสิทธิ์กับ Firebase โดยใช้ผู้ให้บริการ OAuth เช่น Microsoft Azure Active Directory ได้โดยการรวมการเข้าสู่ระบบ OAuth ทั่วไปบนเว็บเข้ากับแอปของคุณโดยใช้ Firebase SDK เพื่อดำเนินการขั้นตอนการลงชื่อเข้าใช้ตั้งแต่ต้นจนจบ

ก่อนที่คุณจะเริ่มต้น

ในการลงชื่อเข้าใช้ผู้ใช้โดยใช้บัญชี Microsoft (Azure Active Directory และบัญชี Microsoft ส่วนบุคคล) คุณต้องเปิดใช้งาน Microsoft เป็นผู้ให้บริการลงชื่อเข้าใช้สำหรับโครงการ Firebase ของคุณก่อน:

  1. เพิ่ม Firebase ในโครงการ Apple ของคุณ
  2. ใน คอนโซล Firebase ให้เปิดส่วน การรับรองความถูกต้อง
  3. บนแท็บ วิธีการลงชื่อเข้า ใช้ ให้เปิดใช้งานผู้ให้บริการ Microsoft
  4. เพิ่ม รหัสไคลเอ็นต์ และ รหัสลับไคลเอ็นต์ จากคอนโซลนักพัฒนาซอฟต์แวร์ของผู้ให้บริการรายนั้นไปยังการกำหนดค่าผู้ให้บริการ:
    1. หากต้องการลงทะเบียนไคลเอนต์ Microsoft OAuth ให้ทำตามคำแนะนำใน Quickstart: Register an app with the Azure Active Directory v2.0 endpoint โปรดทราบว่าตำแหน่งข้อมูลนี้รองรับการลงชื่อเข้าใช้โดยใช้บัญชีส่วนบุคคลของ Microsoft เช่นเดียวกับบัญชี Azure Active Directory เรียนรู้เพิ่มเติม เกี่ยวกับ Azure Active Directory v2.0
    2. เมื่อลงทะเบียนแอปกับผู้ให้บริการเหล่านี้ อย่าลืมลงทะเบียนโดเมน *.firebaseapp.com สำหรับโครงการของคุณเป็นโดเมนเปลี่ยนเส้นทางสำหรับแอปของคุณ
  5. คลิก บันทึก

จัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase SDK

หากต้องการจัดการขั้นตอนการลงชื่อเข้าใช้ด้วย Firebase Apple platforms SDK ให้ทำตามขั้นตอนเหล่านี้:

  1. เพิ่มรูปแบบ URL ที่กำหนดเองในโครงการ Xcode ของคุณ:

    1. เปิดการกำหนดค่าโครงการของคุณ: ดับเบิลคลิกที่ชื่อโครงการในมุมมองแบบต้นไม้ด้านซ้าย เลือกแอปของคุณจากส่วน เป้าหมาย จากนั้นเลือกแท็บ ข้อมูล และขยายส่วน ประเภท URL
    2. คลิกปุ่ม + และเพิ่ม ID แอปที่เข้ารหัสเป็นรูปแบบ URL คุณสามารถค้นหา ID แอปที่เข้ารหัสของคุณได้ในหน้า การตั้งค่าทั่วไป ของคอนโซล Firebase ในส่วนสำหรับแอป iOS ของคุณ เว้นช่องอื่นๆ ว่างไว้

      เมื่อเสร็จแล้ว การกำหนดค่าของคุณควรมีลักษณะดังนี้ (แต่มีค่าเฉพาะแอปพลิเคชันของคุณ):

      ภาพหน้าจอของอินเทอร์เฟซการตั้งค่ารูปแบบ URL ที่กำหนดเองของ Xcode
  2. สร้างอินสแตนซ์ของ OAuthProvider โดยใช้รหัสผู้ให้บริการ microsoft.com

    สวิฟต์

        var provider = OAuthProvider(providerID: "microsoft.com")
        

    วัตถุประสงค์-C

        FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"microsoft.com"];
        
  3. ทางเลือก : ระบุพารามิเตอร์ OAuth ที่กำหนดเองเพิ่มเติมที่คุณต้องการส่งพร้อมกับคำขอ OAuth

    สวิฟต์

        provider.customParameters = [
          "prompt": "consent",
          "login_hint": "user@firstadd.onmicrosoft.com"
        ]
        

    วัตถุประสงค์-C

        [provider setCustomParameters:@{@"prompt": @"consent", @"login_hint": @"user@firstadd.onmicrosoft.com"}];
        

    สำหรับพารามิเตอร์ที่ Microsoft รองรับ โปรดดู เอกสารประกอบ Microsoft OAuth โปรดทราบว่าคุณไม่สามารถส่งพารามิเตอร์ที่กำหนดโดย Firebase กับ setCustomParameters พารามิเตอร์เหล่านี้คือ client_id , response_type , redirect_uri , state , scope และ response_mode

    หากต้องการอนุญาตให้เฉพาะผู้ใช้จากผู้เช่า Azure AD รายใดรายหนึ่งเท่านั้นที่ลงชื่อเข้าใช้แอปพลิเคชันได้ สามารถใช้ชื่อโดเมนที่จำง่ายของผู้เช่า Azure AD หรือตัวระบุ GUID ของผู้เช่าได้ ซึ่งทำได้โดยการระบุช่อง "ผู้เช่า" ในออบเจ็กต์พารามิเตอร์ที่กำหนดเอง

    สวิฟต์

        provider.customParameters = [
          // Optional "tenant" parameter in case you are using an Azure AD
          // tenant. eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or
          // 'contoso.onmicrosoft.com' or "common" for tenant-independent
          // tokens. The default value is "common".
          "tenant": "TENANT_ID"
        ]
        

    วัตถุประสงค์-C

        // Optional "tenant" parameter in case you are using an Azure AD tenant.
        // eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or
        // 'contoso.onmicrosoft.com' or "common" for tenant-independent tokens.
        // The default value is "common".
        provider.customParameters = @{@"tenant": @"TENANT_ID"};
        
  4. ทางเลือก : ระบุขอบเขต OAuth 2.0 เพิ่มเติมนอกเหนือจากโปรไฟล์พื้นฐานที่คุณต้องการร้องขอจากผู้ให้บริการตรวจสอบสิทธิ์

    สวิฟต์

        provider.scopes = ["mail.read", "calendars.read"]
        

    วัตถุประสงค์-C

        [provider setScopes:@[@"mail.read", @"calendars.read"]];
        

    หากต้องการเรียนรู้เพิ่มเติม โปรดดู เอกสารสิทธิ์และความยินยอมของ Microsoft

  5. ทางเลือก : หากคุณต้องการปรับแต่งวิธีที่แอปของคุณนำเสนอ SFSafariViewController หรือ UIWebView เมื่อแสดง reCAPTCHA แก่ผู้ใช้ ให้สร้างคลาสแบบกำหนดเองที่สอดคล้องกับโปรโตคอล AuthUIDelegate และส่งต่อไปยัง credentialWithUIDelegate

  6. รับรองความถูกต้องกับ Firebase โดยใช้วัตถุผู้ให้บริการ OAuth

    สวิฟต์

        // Replace nil with the custom class that conforms to AuthUIDelegate
        // you created in last step to use a customized web view.
        provider.getCredentialWith(nil) { credential, error in
          if error != nil {
            // Handle error.
          }
          if credential != nil {
            Auth().signIn(with: credential) { authResult, error in
              if error != nil {
                // Handle error.
              }
              // User is signed in.
              // IdP data available in authResult.additionalUserInfo.profile.
              // OAuth access token can also be retrieved:
              // (authResult.credential as? OAuthCredential)?.accessToken
              // OAuth ID token can also be retrieved:
              // (authResult.credential as? OAuthCredential)?.idToken
            }
          }
        }
        

    วัตถุประสงค์-C

        [provider getCredentialWithUIDelegate:nil
                                   completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) {
          if (error) {
           // Handle error.
          }
          if (credential) {
            [[FIRAuth auth] signInWithCredential:credential
                                      completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
              if (error) {
                // Handle error.
              }
              // User is signed in.
              // IdP data available in authResult.additionalUserInfo.profile.
              // OAuth access token can also be retrieved:
              // ((FIROAuthCredential *)authResult.credential).accessToken
              // OAuth ID token can also be retrieved:
              // ((FIROAuthCredential *)authResult.credential).idToken
            }];
          }
        }];
        

    เมื่อใช้โทเค็นการเข้าถึง OAuth คุณสามารถเรียกใช้ Microsoft Graph API

    ตัวอย่างเช่น หากต้องการรับข้อมูลโปรไฟล์พื้นฐาน คุณสามารถเรียกใช้ REST API โดยส่งโทเค็นการเข้าถึงในส่วนหัว Authorization :

    https://graph.microsoft.com/v1.0/me
    

    ซึ่งแตกต่างจากผู้ให้บริการรายอื่นที่สนับสนุนโดย Firebase Auth Microsoft ไม่ได้ให้ URL ของรูปถ่าย แต่จะต้องขอข้อมูลไบนารีสำหรับรูปโปรไฟล์ผ่าน Microsoft Graph API แทน

    นอกจากโทเค็นการเข้าถึง OAuth แล้ว โทเค็นรหัส OAuth ของผู้ใช้ยังสามารถเรียกค้นได้จากวัตถุ OAuthCredential การอ้างสิทธิ์ sub ในโทเค็น ID เป็นแบบเฉพาะแอปและจะไม่ตรงกับตัวระบุผู้ใช้แบบรวมศูนย์ที่ใช้โดย Firebase Auth และเข้าถึงได้ผ่าน user.providerData[0].uid ควรใช้ช่องอ้างสิทธิ์ oid แทน เมื่อใช้ผู้เช่า Azure AD เพื่อลงชื่อเข้าใช้ การอ้างสิทธิ์ oid จะตรงกันทุกประการ อย่างไรก็ตาม สำหรับกรณีที่ไม่ใช่ผู้เช่า ฟิลด์ oid จะถูกเพิ่มเข้าไป สำหรับ ID ส่วนกลาง 4b2eabcdefghijkl oid จะมีรูปแบบ 00000000-0000-0000-4b2e-abcdefghijkl

  7. แม้ว่าตัวอย่างข้างต้นจะมุ่งเน้นไปที่ขั้นตอนการลงชื่อเข้าใช้ คุณยังมีความสามารถในการเชื่อมโยงผู้ให้บริการของ Microsoft กับผู้ใช้ที่มีอยู่โดยใช้ linkWithCredential ตัวอย่างเช่น คุณสามารถเชื่อมโยงผู้ให้บริการหลายรายกับผู้ใช้รายเดียวกัน โดยอนุญาตให้พวกเขาลงชื่อเข้าใช้ด้วยผู้ให้บริการรายใดรายหนึ่ง

    สวิฟต์

        Auth().currentUser.link(withCredential: credential) { authResult, error in
          if error != nil {
            // Handle error.
          }
          // Microsoft credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // OAuth access token can also be retrieved:
          // (authResult.credential as? OAuthCredential)?.accessToken
          // OAuth ID token can also be retrieved:
          // (authResult.credential as? OAuthCredential)?.idToken
        }
        

    วัตถุประสงค์-C

        [[FIRAuth auth].currentUser
            linkWithCredential:credential
                    completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
          if (error) {
            // Handle error.
          }
          // Microsoft credential is linked to the current user.
          // IdP data available in authResult.additionalUserInfo.profile.
          // OAuth access token can also be retrieved:
          // ((FIROAuthCredential *)authResult.credential).accessToken
          // OAuth ID token can also be retrieved:
          // ((FIROAuthCredential *)authResult.credential).idToken
        }];
        
  8. สามารถใช้รูปแบบเดียวกันนี้กับ reauthenticateWithCredential ซึ่งสามารถใช้เพื่อดึงข้อมูลรับรองใหม่สำหรับการดำเนินการที่ละเอียดอ่อนซึ่งต้องมีการเข้าสู่ระบบล่าสุด

    สวิฟต์

        Auth().currentUser.reauthenticateWithCredential(withCredential: credential) { authResult, error in
          if error != nil {
            // Handle error.
          }
          // User is re-authenticated with fresh tokens minted and
          // should be able to perform sensitive operations like account
          // deletion and email or password update.
          // IdP data available in result.additionalUserInfo.profile.
          // Additional OAuth access token can also be retrieved:
          // (authResult.credential as? OAuthCredential)?.accessToken
          // OAuth ID token can also be retrieved:
          // (authResult.credential as? OAuthCredential)?.idToken
        }
        

    วัตถุประสงค์-C

        [[FIRAuth auth].currentUser
            reauthenticateWithCredential:credential
                              completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
          if (error) {
            // Handle error.
          }
          // User is re-authenticated with fresh tokens minted and
          // should be able to perform sensitive operations like account
          // deletion and email or password update.
          // IdP data available in result.additionalUserInfo.profile.
          // Additional OAuth access token can also be retrieved:
          // ((FIROAuthCredential *)authResult.credential).accessToken
          // OAuth ID token can also be retrieved:
          // ((FIROAuthCredential *)authResult.credential).idToken
        }];
        

หลังจากที่ผู้ใช้ลงชื่อเข้าใช้เป็นครั้งแรก บัญชีผู้ใช้ใหม่จะถูกสร้างขึ้นและเชื่อมโยงกับข้อมูลรับรอง ซึ่งได้แก่ ชื่อผู้ใช้และรหัสผ่าน หมายเลขโทรศัพท์ หรือข้อมูลผู้ให้บริการรับรองความถูกต้อง ซึ่งผู้ใช้ลงชื่อเข้าใช้ด้วย บัญชีใหม่นี้จัดเก็บเป็นส่วนหนึ่งของโปรเจ็กต์ Firebase และสามารถใช้ระบุผู้ใช้ในทุกแอปในโครงการ ไม่ว่าผู้ใช้จะลงชื่อเข้าใช้ด้วยวิธีใดก็ตาม

  • ในแอปของคุณ คุณสามารถรับข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ได้จากวัตถุ User ดู จัดการผู้ใช้

  • ใน กฎความปลอดภัย ของ Firebase Realtime Database และ Cloud Storage คุณสามารถรับ ID ผู้ใช้ที่ไม่ซ้ำกันของผู้ใช้ที่ลงชื่อเข้าใช้จากตัวแปร auth และใช้เพื่อควบคุมข้อมูลที่ผู้ใช้สามารถเข้าถึงได้

คุณสามารถอนุญาตให้ผู้ใช้ลงชื่อเข้าใช้แอปของคุณโดยใช้ผู้ให้บริการตรวจสอบสิทธิ์หลายรายโดย เชื่อมโยงข้อมูลประจำตัวของผู้ให้บริการตรวจสอบสิทธิ์กับบัญชีผู้ใช้ที่มีอยู่

หากต้องการออกจากระบบผู้ใช้ ให้เรียก signOut:

สวิฟต์

let firebaseAuth = Auth.auth()
do {
  try firebaseAuth.signOut()
} catch let signOutError as NSError {
  print("Error signing out: %@", signOutError)
}

วัตถุประสงค์-C

NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
  NSLog(@"Error signing out: %@", signOutError);
  return;
}

คุณอาจต้องการเพิ่มรหัสการจัดการข้อผิดพลาดสำหรับข้อผิดพลาดการตรวจสอบสิทธิ์ทั้งหมด ดู จัดการข้อผิดพลาด