如何在Identity Server4中使用ASP.NET Idenity与Azure AD外部登录一起使用

时间:2018-08-10 06:55:03

标签: azure asp.net-core asp.net-identity azure-active-directory identityserver4

我按照下面的文章在IdenityServer4中使用asp.net身份进行工作: http://docs.identityserver.io/en/release/quickstarts/6_aspnet_identity.html

现在,我想将Azure AD添加为外部IDp:

 services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity<ApplicationUser>();


        services.AddAuthentication()
           .AddOpenIdConnect("AAD", "Azure Active Directory", options =>
           {
               options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
               options.SignOutScheme = IdentityServerConstants.SignoutScheme;
               options.Authority = "https://login.microsoftonline.com/xxxxxx.onmicrosoft.com";
               options.ClientId = "xxxxxxx";
               options.Scope.Add("openid");
               options.Scope.Add("profile");
               options.TokenValidationParameters = new TokenValidationParameters
               {
                   ValidateIssuer = false
               };
               options.GetClaimsFromUserInfoEndpoint = true;
           });

但是当我调试我的应用程序==>重定向到身份服务器==>单击AAD登录时。我注意到当我到达Azure AD用户名/密码页面时,我的客户端应用程序停止了,因此在输入凭据并同意后,我将停留在身份服务器页面上。谁能提供一些建议?

2 个答案:

答案 0 :(得分:0)

我看到您注释了您的Microsoft Authority URL,就像它是针对您公司的。我已为公司的身份验证配置了Azure AD外部登录,而我只需要致电https://login.microsoftonline.com/common

此外,如果它可以正常重定向到Microsoft的站点并且没有返回到Auth服务器,则您的控制器中的ExternalLogin函数中可能未正确设置某些内容。

它应该看起来像这样:

var props = new AuthenticationProperties()
{
    RedirectUri = Url.Action("ExternalLoginCallback"),
    Items =
    {
        { "returnUrl", returnUrl },
        { "scheme", provider },
    }
};
return Challenge(props, provider);

提供商将是您的“ AAD”。

答案 1 :(得分:0)

有趣的部分是,当我转向使用chrome在Visual Studio中启动我的客户端应用程序时,该客户端应用程序不再停止。非常奇怪,没有找到解释。