代码之家  ›  专栏  ›  技术社区  ›  Jayendran

OpenIDConnect.NET框架中的多租户

  •  1
  • Jayendran  · 技术社区  · 5 年前

    我试图实现多租户身份验证(我正在学习),到目前为止,我已经成功地在单租户中实现了应用程序的身份验证。

    我为单身房客使用的代码是

     public void ConfigureAuth(IAppBuilder app)
            {
    
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
                app.UseCookieAuthentication(new CookieAuthenticationOptions());
                app.UseOpenIdConnectAuthentication(
                     new OpenIdConnectAuthenticationOptions
                     {
                         ClientId = ConfigurationManager.AppSettings["AuthclientId"],
                         Authority = "https://login.microsoftonline.com/abc.onmicrosoft.com/",
    
    
    
                     });
            }
    

    这里,首先我把我的申请登记在 ABC aad并获取客户机ID,然后将其设置为我的配置。一切都很好。

    但现在我必须用多租户类型来实现这一点。尽管它是多租户的,但我只允许2个租户用户。让我们说 abc.onmicrosoft.com contoso.onmicrosoft.com

    到目前为止,我喜欢在 基础知识 租户和 Contoso 然后租户会得到2个客户端ID。但是我的问题是无法在 UseOpenIdConnectAuthentication (见下面我的更新代码)

    public void ConfigureAuth(IAppBuilder app)
            {
    
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
                app.UseCookieAuthentication(new CookieAuthenticationOptions());
                app.UseOpenIdConnectAuthentication(
                     new OpenIdConnectAuthenticationOptions
                     {
    
                         ClientId = ??,
                         Authority = "https://login.microsoftonline.com/common/",
                         TokenValidationParameters = new TokenValidationParameters
                         {
                             ValidateIssuer = false
                         },
    
                     });
            }
    

    这对我来说是新的。我可能错了,请纠正我以使事情走上正确的道路

    更新1:

    app.UseOpenIdConnectAuthentication(
                 new OpenIdConnectAuthenticationOptions
                 {
    
                     //ClientId = authClientID1,//App ID registered with 1st Tenant
                     Authority = "https://login.microsoftonline.com/common/",
                     RedirectUri= "https://localhost:44376/",
                     TokenValidationParameters = new TokenValidationParameters
                     {
                         ValidAudiences = new List<string>{ authClientID1, authClientID2 },
                         ValidateIssuer =true,
                         ValidIssuers= new[] { "https://sts.windows.net/<tenantID1>/", "https://sts.windows.net/<tenantID2>/" }
                     },
    
                 });
    

    在评论了clientid之后,我收到了错误 aadsts900144:请求主体必须包含以下参数: “客户”

    我不知道如何给出我的两个clientid和tenant id,以便仅从我的两个租户对用户进行身份验证!

    2 回复  |  直到 5 年前
        1
  •  0
  •   Alex AIT    5 年前

    要交付多租户应用程序,您只能在AAD中创建一个应用程序。所以你也只有 一个客户身份 . 确保您的应用程序已启用“多租户”。

    您可以在这里找到很多信息: https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-convert-app-to-be-multi-tenant

    还提供了完整的样品: https://github.com/Azure-Samples/active-directory-dotnet-webapp-multitenant-openidconnect

       public void ConfigureAuth(IAppBuilder app)
        {         
            string ClientId = ConfigurationManager.AppSettings["ida:ClientID"];
            //fixed address for multitenant apps in the public cloud
            string Authority = "https://login.microsoftonline.com/common/";
    
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    
            app.UseCookieAuthentication(new CookieAuthenticationOptions { });
    
            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = ClientId,
                    Authority = Authority,
                    TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                    {
                        // instead of using the default validation (validating against a single issuer value, as we do in line of business apps), 
                        // we inject our own multitenant validation logic
                        ValidateIssuer = false,
                    },
                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        RedirectToIdentityProvider = (context) =>
                        {
                            // This ensures that the address used for sign in and sign out is picked up dynamically from the request
                            // this allows you to deploy your app (to Azure Web Sites, for example)without having to change settings
                            // Remember that the base URL of the address used here must be provisioned in Azure AD beforehand.
                            string appBaseUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.PathBase;                         
                            context.ProtocolMessage.RedirectUri = appBaseUrl;
                            context.ProtocolMessage.PostLogoutRedirectUri = appBaseUrl;
                            return Task.FromResult(0);
                        },
                        // we use this notification for injecting our custom logic
                        SecurityTokenValidated = (context) =>
                        {
                            // retriever caller data from the incoming principal
                            string issuer = context.AuthenticationTicket.Identity.FindFirst("iss").Value;
                            string UPN = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Name).Value;
                            string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
    
                            if (
                                // the caller comes from an admin-consented, recorded issuer
                                (db.Tenants.FirstOrDefault(a => ((a.IssValue == issuer) && (a.AdminConsented))) == null)
                                // the caller is recorded in the db of users who went through the individual onboardoing
                                && (db.Users.FirstOrDefault(b =>((b.UPN == UPN) && (b.TenantID == tenantID))) == null)
                                )
                                // the caller was neither from a trusted issuer or a registered user - throw to block the authentication flow
                                throw new SecurityTokenValidationException();                            
                            return Task.FromResult(0);
                        },
                        AuthenticationFailed = (context) =>
                        {
                            context.OwinContext.Response.Redirect("/Home/Error?message=" + context.Exception.Message);
                            context.HandleResponse(); // Suppress the exception
                            return Task.FromResult(0);
                        }
                    }
                });
    
        }
    
        2
  •  1
  •   juunas    5 年前

    您的客户端ID应该是您的应用程序客户端ID。您不会在其他租户中创建其他应用程序。将权力设定为共同点就足够了。如果要允许任何租户,可以禁用颁发者验证。

    然后,当其他租户的某个人登录到您的应用程序时,会要求他们同意您所需的权限。一旦他们这样做了,就会在他们的租户中自动创建代表您的应用程序的服务主体。它具有相同的客户端ID。