繁体   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