[英]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.cs
的public 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中間件執行以下操作:
"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.