无法登录多租户应用程序

Unable to login in multitenant application

我对 Azure Ad 多租户身份验证有些困惑。

我的应用程序是 Visual Studio 2019 年的 Devexpress XAF Blazor 应用程序。

Devexpress 版本 21.2.3

我想要 azure ad 多租户身份验证,单租户身份验证工作正常。

我已经关注了以下文档:-

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'.

Error screen shot

当我使用下面的代码时

 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 Ad 多租户应用程序中使用了哪个。

你好,

  1. AddAzureAD 似乎已过时。请注意,我的应用程序设置(TenadID、ClientId...)根本不起作用
  2. 我更换的时候也有同样的问题 "实例": "https://login.microsoftonline.com", 到 "实例": "https://login.microsoftonline.com/common",

我建议你上传 debug simbols 也看到 HttpDocumentRetriever.GetDocumentAsync

中的 exat 问题

对我来说是 StatusCode 400 或 404 在 VisualStudio 中转到:工具->选项->调试->符号。

Error response content

“无法注销”的第二个问题

  1. 你设置的默认sheme是AzureADDefaults.AuthenticationScheme
  2. AzureAD 使用 cookie 方案来存储用户数据。当然这是默认设置,您可以更改它。
  3. 当您调用 LogOut 时,xaf 应用程序会调用 等待 context.SignOutAsync(); 这意味着使用默认方案,但在这种情况下默认方案是“AzureADDefaults.AuthenticationScheme”。饼干还在你身边。

我不确定您是否需要将“AzureADDefaults.AuthenticationScheme”作为默认方案,或者我不知道这样做的原因。

当您需要一些复杂的解决方案并且 XAF 无法使用现成的解决方案时,最好尝试不使用 XAF 的身份验证。

当然你可以在他们执行的地方覆盖 XAF 注销逻辑 context.SignOutAsync() 由您自己。他们为此使用中间件,你可以自己编写并在 XAF 中间件注册之前注册它,之前

app.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 团队建议的正确设置得到解决。

  "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);