【问题标题】:Microsoft Active Directory authentication Single tenant "Live.com"Microsoft Active Directory 身份验证单租户“Live.com”
【发布时间】:2018-02-10 14:04:52
【问题描述】:

我已实施 AD 身份验证,其中我已将 Client Id、redirectUri 和 Tenant 作为“Common”传递。由于我已将租户用作 live.com、outlook.com、microsoft.com 以及学校和办公室的“普通”用户。我希望它仅限于 Live.com 用户。

public class Startup
{
    // The Client ID is used by the application to uniquely identify itself to Azure AD.
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];

    // RedirectUri is the URL where the user will be redirected to after they sign in.
    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

    // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant)
    static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];

    // Authority is the URL for authority, composed by Azure Active Directory v2 endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0)
    string authority = "https://login.microsoftonline.com/common/v2.0" ;

    /// <summary>
    /// Configure OWIN to use OpenIdConnect 
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(

        new OpenIdConnectAuthenticationOptions
        {
            // Sets the ClientId, authority, RedirectUri as obtained from web.config
            ClientId = clientId,
            Authority = authority,
            RedirectUri = redirectUri,
            // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
            PostLogoutRedirectUri = "https://localhost:44368/Claims/Register",
            Scope = OpenIdConnectScopes.OpenIdProfile,
            // ResponseType is set to request the id_token - which contains basic information about the signed-in user
            ResponseType = OpenIdConnectResponseTypes.IdToken,
            // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
            // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
            // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter 
            TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = false },

            // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = OnAuthenticationFailed,
                AuthorizationCodeReceived = (c) => {
                    var code = c.Code;
                    return Task.FromResult(0);
                }
            }
        }
    );
    }

    /// <summary>
    /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
    {
        context.HandleResponse();
        context.Response.Redirect("/?errormessage=" + context.Exception.Message);
        return Task.FromResult(0);
    }
}
}

【问题讨论】:

    标签: authentication azure-active-directory multi-tenant


    【解决方案1】:

    来自Azure AD's v2.0 endpoint docs

    注册后,应用通过向 v2.0 端点发送请求与 Azure AD 进行通信:

    https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token

    {tenant} 可以采用四个不同值之一:

    • common - 允许同时拥有个人 Microsoft 帐户和 Azure Active Directory 工作/学校帐户的用户登录应用程序。
    • organizations - 仅允许拥有 Azure Active Directory 中的工作/学校帐户的用户登录应用程序
    • consumers - 仅允许拥有个人 Microsoft 帐户 (MSA) 的用户登录应用程序。
    • 8eaef023-2b34-4da1-9baa-8bc8c9d6a490contoso.onmicrosoft.com - 仅允许具有来自特定 Azure Active Directory 租户的工作/学校帐户的用户登录应用程序。可以使用 Azure AD 租户的友好域名或租户的 guid 标识符。

    如果您想进一步限制到消费者域(@live.com 与 @outlook.com),您需要在应用程序级别自行实现,查看 email 声明。 请注意,通常这种级别的过滤没有多大意义,因为 live.com 帐户和 outlook.com 帐户之间没有功能/实际差异,它们只是具有不同的虚域。

    【讨论】:

    • 很好的答案,如果它调用您自己的 Web API,则添加另一种方法来限制对您的应用程序的访问。您的 Web API 可以查看颁发的令牌中的 iss 声明。 Microsoft 帐户用户将在此处显示唯一的租户 ID,您的后端可以验证和限制对其的访问。查看Azure AD v2.0 token reference 以获取有关令牌声明的帮助。如需样品列表,请查看Azure AD v2.0 landing page
    • 这正是我想要的。我可以使用此 AAD 端点 V2 进行 API 身份验证吗。
    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多