繁体   English   中英

使用Azure Active Directory单一登录

[英]Single Sign On with Azure Active Directory

我创建了许多Web应用程序。 其中一些使用旧式Web表单身份验证,而我们公司的其他一些站点则使用不同的身份验证模式。

我正在尝试使用Azure Active Directory以一种SSO模式将其合并。 我一直在尝试按照指南/教程进行操作,但并不是针对我。

我的技术目前是ASP 4 / MVC 5,尽管如果ASP 5 / MVC 6更简单,那么我也可以自由选择。 当前,所有Web应用程序都托管在Azure中。

我的困惑在于,在浏览文档时,似乎有太多的方法可以对用户进行身份验证和授权(而且,对我而言,身份验证与授权并不是绝对清楚的)。

我去了Azure管理门户(旧的)的Active Directory区域。 我添加了一个名为TestApp的新应用程序。 我将应用程序URL设置为https://localhost:44320/ ,然后将登录URL设置为https://localhost:44320/ ,租户名称为testapp 我的回复URL是https://localhost:44320/

这使我的应用程序ID uri https://localhost:44320/testapp我觉得呢? 我也有我的客户ID guid。

本教程的AccountController具有如下SignIn方法:

public void SignIn()
{
    // Send an OpenID Connect sign-in request.
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
    }
}

导航至此时,我在浏览器中收到以下信息:

[InvalidOperationException:IDX10803:无法创建以从以下位置获取配置:' https:// localhost:44320 / testapp / .well-known / openid-configuration '。]

我感觉是因为Azure无法将所有这些重定向到我的本地主机? 我如何设置它以便可以在Azure本身上进行测试? 甚至更进一步,该解决方案是否可以在多个Web应用程序中使用? 我假设它们在Active Directory中都是不同的应用程序,但是它们都需要使用SSO过程,用户可以在其中使用一个身份登录多个应用程序。

抱歉让您感到困惑,这一切对我来说都是非常令人费解的,但是我正在努力学习。

编辑:

在webapp的启动中,我将其称为:

private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions());

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = authority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = context => 
                {
                    context.HandleResponse();
                    context.Response.Redirect("/Error?message=" + context.Exception.Message);
                    return Task.FromResult(0);
                }
            }
        });
}

利用以下app.config设置:

<add key="ida:ClientId" value="[my client id here]" />
<add key="ida:Tenant" value="testapp" />
<add key="ida:AADInstance" value="https://localhost:44320/{0}" />
<add key="ida:PostLogoutRedirectUri" value="https://localhost:44320/" />

Azure能够重定向到本地主机,它将仅弹出一个安全确认,询问是否可以导航到本地主机。

您在app.config中的租户看起来不正确,请更改以下应用程序设置:

<add key="ida:Tenant" value="[YOUR TENANT].onmicrosoft.com" />
<add key="ida:AADInstance" value="https://login.microsoftonline.com/{0}" />

若要查找有关您的租户的更多信息,请参见本文: 如何获取Azure Active Directory租户

您也可以尝试将此代码添加到“启动”中的“通知”中(就在AuthenticationFailed下),尝试在处理程序上放置断点以查看会发生什么:

AuthenticationFailed = context => 
                {
                    context.HandleResponse();
                    context.Response.Redirect("/Error?message=" + context.Exception.Message);
                    return Task.FromResult(0);
                },     
SecurityTokenValidated = (context) =>
                {

                    return Task.FromResult(0);
                }

在您的一个控制器上放置一个[Authorize]属性,当您浏览到它时,它应该重定向到AAD身份验证。

AFAIK每个应用程序在Azure AD中都需要一个单独的应用程序,并且您需要在每个单独的应用程序中实现此身份验证。 通过URL从一个应用程序链接到另一个应用程序时,我设法获得了无缝登录的体验。

这个答案很好地总结了身份验证与授权: 身份验证与授权

暂无
暂无

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

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