共用方式為


呼叫 Web API 的桌面應用程式:移至生產環境

在本文中,您將瞭解如何將呼叫 Web API 的桌面應用程式移至生產環境。

處理傳統型應用程式中的錯誤

在不同的流程中,您已瞭解如何處理無訊息流程的錯誤,如代碼段所示。 您也看到有需要互動的情況,如同累加式同意和條件式存取。

注意

取得數個資源的同意適用於 Microsoft 身分識別平台,但不適用於 Azure Active Directory (Azure AD) B2C。 Azure AD B2C 僅支持系統管理員同意,不支持使用者同意。

您無法使用 Microsoft 身分識別平台 一次取得數個資源的令牌。 參數 scopes 只能包含單一資源的範圍。 您可以使用 參數,確保用戶預先同意數個資源 extraScopesToConsent

例如,您可能有兩個資源各自有兩個範圍:

  • https://mytenant.onmicrosoft.com/customerapi 具有範圍 customer.readcustomer.write
  • https://mytenant.onmicrosoft.com/vendorapi 具有範圍 vendor.readvendor.write

在此範例中,請使用 .WithExtraScopesToConsent 具有 參數的 extraScopesToConsent 修飾詞。

例如:

在 MSAL.NET

string[] scopesForCustomerApi = new string[]
{
  "https://mytenant.onmicrosoft.com/customerapi/customer.read",
  "https://mytenant.onmicrosoft.com/customerapi/customer.write"
};
string[] scopesForVendorApi = new string[]
{
 "https://mytenant.onmicrosoft.com/vendorapi/vendor.read",
 "https://mytenant.onmicrosoft.com/vendorapi/vendor.write"
};

var accounts = await app.GetAccountsAsync();
var result = await app.AcquireTokenInteractive(scopesForCustomerApi)
                     .WithAccount(accounts.FirstOrDefault())
                     .WithExtraScopesToConsent(scopesForVendorApi)
                     .ExecuteAsync();

在適用於 iOS 和 macOS 的 MSAL 中

Objective-C:

NSArray *scopesForCustomerApi = @[@"https://mytenant.onmicrosoft.com/customerapi/customer.read",
                                @"https://mytenant.onmicrosoft.com/customerapi/customer.write"];

NSArray *scopesForVendorApi = @[@"https://mytenant.onmicrosoft.com/vendorapi/vendor.read",
                              @"https://mytenant.onmicrosoft.com/vendorapi/vendor.write"]

MSALInteractiveTokenParameters *interactiveParams = [[MSALInteractiveTokenParameters alloc] initWithScopes:scopesForCustomerApi webviewParameters:[MSALWebviewParameters new]];
interactiveParams.extraScopesToConsent = scopesForVendorApi;
[application acquireTokenWithParameters:interactiveParams completionBlock:^(MSALResult *result, NSError *error) { /* handle result */ }];

Swift:

let scopesForCustomerApi = ["https://mytenant.onmicrosoft.com/customerapi/customer.read",
                            "https://mytenant.onmicrosoft.com/customerapi/customer.write"]

let scopesForVendorApi = ["https://mytenant.onmicrosoft.com/vendorapi/vendor.read",
                          "https://mytenant.onmicrosoft.com/vendorapi/vendor.write"]

let interactiveParameters = MSALInteractiveTokenParameters(scopes: scopesForCustomerApi, webviewParameters: MSALWebviewParameters())
interactiveParameters.extraScopesToConsent = scopesForVendorApi
application.acquireToken(with: interactiveParameters, completionBlock: { (result, error) in /* handle result */ })

此呼叫會取得第一個 Web API 的存取令牌。

呼叫第二個 Web API 時,請呼叫 AcquireTokenSilent API。

AcquireTokenSilent(scopesForVendorApi, accounts.FirstOrDefault()).ExecuteAsync();

每次執行應用程式時,Microsoft 個人帳戶都需要重新對應

針對 Microsoft 個人帳戶使用者,針對每個原生用戶端 (桌面或行動應用程式) 呼叫重新發出同意,以授權是預期的行為。 原生用戶端身分識別原本就不安全,這與機密用戶端應用程式身分識別相反。 機密用戶端應用程式會與 Microsoft 身分識別平台 交換秘密,以證明其身分識別。 Microsoft 身分識別平台 選擇在每次授權應用程式時提示使用者同意,以減輕消費者服務的這種不安全感。

啟用 記錄

為了協助偵錯和驗證失敗疑難解答案例,Microsoft 驗證連結庫提供內建記錄支援。 下列文章涵蓋每個連結庫中的記錄:

以下是數據收集的一些建議:

  • 使用者可能會在有問題時尋求協助。 最佳做法是擷取並暫時儲存記錄。 提供使用者可以上傳記錄的位置。 MSAL 提供記錄延伸模組來擷取驗證的詳細資訊。

  • 如果遙測可供使用,請透過 MSAL 來收集數據,以了解使用者如何登入您的應用程式。

驗證您的整合

遵循 Microsoft 身分識別平台 整合檢查清單來測試您的整合。

為復原能力而建置

瞭解如何增加應用程式中的復原能力。 如需詳細資訊,請參閱 增加您所開發之驗證和授權應用程式的復原能力

下一步

若要試用其他範例,請參閱 桌面公用用戶端應用程式