繁体   English   中英

如何在多租户应用中通过C#以编程方式切换Azure AD目录

[英]How to switch Azure AD directory programatically via C# in a multi tenant app

我有配置有多租户Azure AD应用程序的ASP.NET MVC应用程序。 因此,在我的Web.config我必须将<add key="ida:Authority" value="https://login.microsoftonline.com/common/" />放入,以便对任何人都有效。 但是现在我的某些应用程序用户被分配到了不同的Azure目录(租户ID有所不同)。 我需要提供“交换目录”功能,例如在Azure门户/ Aure IoT Central中。

我知道如果我用租户ID替换common ,则该应用程序仅适用于特定的租户。

Startup.Auth.cspublic void ConfigureAuth(IAppBuilder app) ,我有以下内容

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions { });
    app.UseCookieAuthentication(new CookieAuthenticationOptions ());

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            ClientSecret = clientSecret,
            Authority = authority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
            RedirectUri = redirectUri,

            TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
            {
                ValidateIssuer = false,
            },
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = (context) =>
                {
                    string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                    // The above tenantID is always coming from the default directory
                    string issuer = context.AuthenticationTicket.Identity.FindFirst("iss").Value;
                    // The above is also always from the default directory (tenant id)
                    string UPN = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.Name).Value;


                    return Task.FromResult(0);
                },
                AuthorizationCodeReceived = (context) =>
                {
                    var code = context.Code;
                    HttpContext.Current.Session["CODE"] = code;
                    return Task.FromResult(0);
                },
                AuthenticationFailed = context =>
                {
                    return Task.FromResult(0);
                }
            }
        });
}

因此,这为我提供了一个OWIN上下文,该上下文仅包含默认目录ID的详细信息。 谁能帮助我改变这种行为? 这样我就可以提供在目录之间切换的功能。

我曾经(使用OWIN)进行过一次黑客攻击,是在请求登录时指定租户ID时添加特定的查询参数。

我现在找不到源代码,但是OWIN中间件执行以下操作:

  • 呼叫下一个中间件
  • 响应是GET,响应状态码是302,位置标头值具有Azure AD主机,并且存在租户ID查询参数吗?
  • 如果是,请抓住Location标头,将值切换为"https://login.microsoftonline.com/tenant-id/oauth2/authorize?" + locationHeader query string "https://login.microsoftonline.com/tenant-id/oauth2/authorize?" + locationHeader query string

这是一种黑客手段,但是可以帮助用户登录到正确的租户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM