簡體   English   中英

無法登錄多租戶應用程序

[英]Unable to login in multitenant application

我對 Azure 廣告多租戶身份驗證有些困惑。

我的應用程序是 Visual Studio 2019 中的 Devexpress XAF Blazor 應用程序。

Devexpress 版本 21.2.3

我想要 azure 廣告多租戶身份驗證,單租戶身份驗證工作正常。

我已經遵循以下文件:-

https://docs.microsoft.com/en-us/azure/architecture/multitenant-identity/

https://itnext.io/why-you-should-be-using-azure-multi-tenant-apps-49d4704b926e

https://docs.devexpress.com/eXpressAppFramework/402197/data-security-and-safety/security-system/authentication/active-directory-and-oauth2-authentication-providers-in-blazor-applications

我的 Azure 廣告配置如下:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/common",
    //"Instance": "https://login.microsoftonline.com",
    "AppIDURL": "https://Mydomain.onmicrosoft.com/MyApp",
    "Domain": "my Domain",
    "TenantId": "My Tenant Id",
    "ClientId": "My Client Id",
    "ClientCertificates": [],
    "CallbackPath": "/signin-oidc"
  },

當我在 startup.cs 文件中使用以下代碼時

  var authentication = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
            authentication
                .AddCookie(options =>
                {
                    options.LoginPath = "/LoginPage";
                }).AddMicrosoftIdentityWebApp(Configuration, configSectionName: "AzureAd", cookieScheme: null);

出現以下錯誤:-

IOException: IDX20807 :無法從“System.String”檢索文檔。 HttpResponseMessage:'System.Net.Http.HttpResponseMessage',HttpResponseMessage.Content:'System.String'。

錯誤截圖

或者

當我使用下面的代碼

 var authentication = services.AddAuthentication(AzureADDefaults.AuthenticationScheme);
            authentication
                .AddCookie(options =>
                {
                    options.LoginPath = "/LoginPage";
                }).AddAzureAD(options => Configuration.Bind("AzureAd", options)); 

我能夠登錄到應用程序,但無法退出它再次登錄的應用程序,並且 Devexpress 登錄頁面也不可見(如上所述 LoginPath)。

我們有多種身份驗證方案,如下所示:-

  1. CookieAuthenticationDefaults.AuthenticationScheme
  2. AzureADDefaults.AuthenticationScheme
  3. OpenIdConnectDefaults.AuthenticationScheme

但是哪一個用於 Azure 廣告多租戶應用程序。

你好,

  1. 看起來 AddAzureAD 已過時。 對我來說,它根本不適用於我的應用程序設置(TenadID,ClientId ...)
  2. 當我將“實例”:“https://login.microsoftonline.com”替換為“實例”:“https://login.microsoftonline.com/common”時,我遇到了同樣的問題,

我建議你上傳調試符號也看到 HttpDocumentRetriever.GetDocumentAsync 中的 exat 問題

對我來說,它是 VisualStudio go 中的 StatusCode 400 或 404 到:工具->選項->調試->符號。

錯誤響應內容

“無法注銷”的第二個問題

  1. 您將默認 sheme 設置為 AzureADDefaults.AuthenticationScheme
  2. AzureAD 使用 cookie 方案來存儲用戶數據。 當然,它是默認設置,您可以更改它。
  3. 當您調用 LogOut 時,xaf 應用程序調用 await context.SignOutAsync(); 這意味着使用默認方案,但在這種情況下,默認方案是“AzureADDefaults.AuthenticationScheme”。 餅干還在你身邊。

我不確定您需要將“AzureADDefaults.AuthenticationScheme”作為默認方案,否則我不知道這樣做的理由。

當您需要一些復雜的解決方案並且 XAF 無法直接使用該解決方案時,最好嘗試不使用 XAF 的身份驗證。

當然,您可以覆蓋 XAF 注銷邏輯,他們自己執行 context.SignOutAsync()。 他們為此使用中間件,您可以編寫自己的並在 XAF 中間件注冊之前注冊它,之前

應用程序.UseXaf();

你的中間件看起來像

using System;
using System.Threading.Tasks;
using DevExpress.ExpressApp.Blazor.Services;
using DevExpress.ExpressApp.Blazor.Utils;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace MyApplication {
    public class CustomSignInMiddleware {
        private readonly RequestDelegate next;
        public CustomSignInMiddleware(RequestDelegate next) {
            this.next = next;
        }
        public async Task Invoke(HttpContext context, ILogger<CustomSignInMiddleware> logger = null) {
            string requestPath = context.Request.Path.Value.TrimStart('/');
            string returnUrl = ReturnUrlHelper.ExtractReturnUrl(context.Request);
             if(requestPath.StartsWith(SignInMiddlewareDefaults.SignOutEndpointName, StringComparison.Ordinal)) {
                await context.SignOutAsync();
                context.Response.Redirect(returnUrl);
            }
            else {
                await next(context);
            }
        }
    }
}

並將 SignOutAsync 與所需方案一起使用。 不要忘記注冊

        app.UseMiddleware<CustomSignInMiddleware>();
        app.UseXaf();

謝謝,迪瑪的回復,

但問題已通過 Microsoft Team 建議的正確設置得到解決。

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "Mydomain",
    "ClientId": "My Client Id",
    "TenantId": "organizations", // It is must in Multi Tenant application 
    "CallbackPath": "/signin-oidc"
  },

我的啟動文件如下

   services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
            {
                options.LoginPath = "/LoginPage";
            }).AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"), cookieScheme: null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM