【问题标题】:Single tenant to multi-tenant单租户到多租户
【发布时间】:2018-10-19 10:50:58
【问题描述】:

我有一个只有一个租户的网站 MVC 5。我使用单租户,但有些用户使用不同的租户登录我的网站。

我有这个错误:AADSTS50020:用户帐户。

你能帮帮我吗?

谢谢。

【问题讨论】:

  • 你必须阅读*.com/help/how-to-ask :)
  • 他们怎么能做到这一点?如果您将您的权限定义为单租户权限(即它包含您的租户 ID),那么他们不应该遇到这个问题(除非他们手动修改 URL)。
  • 能否提供完整的错误信息?
  • error : 需要先在租户中添加账号为外部用户。注销并使用不同的 Azure Active Directory 用户帐户重新登录。
  • 我创建了一个有多个租户的网站,但出现 400 错误...

标签: c# asp.net-mvc-5 azure-active-directory


【解决方案1】:

我有这个错误:AADSTS50020:用户帐户。

如果你没有将你的网站更新为多租户,当其他租户用户想要登录你的网站时,它会提示同样的错误。

我创建了一个包含多个租户的网站,但出现 400 错误。

对于多租户,您需要将端点从租户(如 https://login.microsoftonline.com/contoso.onmicrosoft.com)更改为 common(如 https://login.microsoftonline.com/common)。这样,登录请求可以发送到跨所有 Azure AD 租户多路复用的端点。

详情可参考here

【讨论】:

    【解决方案2】:

    感谢您的回答。

    我必须如何修改我的代码:

      public partial class Startup
    {
        private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
        private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
        private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
        private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
    
        public static readonly string Authority = aadInstance + tenantId;
    
        // This is the resource ID of the AAD Graph API.  We'll need this to request a token to call the Graph API.
        string graphResourceId = "https://graph.windows.net";
    
        public void ConfigureAuth(IAppBuilder app)
        {
            ApplicationDbContext db = new ApplicationDbContext();
    
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
    
            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = Authority,
                    PostLogoutRedirectUri = postLogoutRedirectUri,
    
                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
    
                       AuthorizationCodeReceived = (context) => 
                       {
                           var code = context.Code;
                           ClientCredential credential = new ClientCredential(clientId, appKey);
                           string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                           AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
                           AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                           code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
    
                           return Task.FromResult(0);
                       }
                    }
                });
        }
    }
    

    谢谢

    【讨论】:

    • 如果回答对你有用,请帮忙标记,谢谢!