支持MVC5/ASP.Net Identity 2中的个人用户帐户和组织帐户

Bre*_*een 13 authentication azure asp.net-identity azure-active-directory asp.net-mvc-5.1

我已经创建了一个ASP.Net MVC5应用程序,我已经通过谷歌,Facebook等配置(并且工作正常)个人用户帐户.

我想做的还是支持Azure Active Directory(组织帐户)的身份验证.这将使内部员工能够以管理员身份登录应用程序.

我发现的所有现有信息/指南/文档通常涉及使用其中一个.我如何一起启用它们?

如果需要为每种类型的用户提供单独的登录表单,那么这不是问题.

编辑:

我正在查看Azure Active Directory门户中的应用程序配置,并注意到它们定义了"OAUTH 2.0 AUTHORIZATION ENDPOINT".可以在其中配置MVC5 Startup.Auth.cs来使用它吗?

Bre*_*een 5

我设法通过执行以下操作来实现这一点:

首先,添加对Microsoft.Owin.Security.OpenIdConnectNuget 包的引用。

其次,在我的配置它Startup.Auth.cs

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ClientId = "From the Azure Portal (see below)",
    Authority = "https://login.windows.net/<domain>.onmicrosoft.com",
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
        RedirectToIdentityProvider = (ctx) =>
        {
            if (ctx.Request.Path.Value.EndsWith("ExternalLogin"))
            {
                string appBasePathUrl = ctx.Request.Scheme + "://" + ctx.Request.Host + ctx.Request.PathBase;
                ctx.ProtocolMessage.RedirectUri = appBasePathUrl + "/";
                ctx.ProtocolMessage.PostLogoutRedirectUri = appBasePathUrl;
            }
            else
            {
                ctx.State = NotificationResultState.Skipped;
                ctx.HandleResponse();
            }

            return Task.FromResult(0);
        }
    },
    Description = new AuthenticationDescription
    {
        AuthenticationType = "OpenIdConnect",
        Caption = "SomeNameHere"
    }
});
Run Code Online (Sandbox Code Playgroud)

第三,我在 Azure 门户(经典)中设置应用程序:

Azure Active Directory 应用程序配置

第四,我为管理员用户添加了一个单独的登录页面:

@using (Html.BeginForm("ExternalLogin", "Home"))
{
    @Html.AntiForgeryToken()
    <div class="ui basic segment">
        <div class="ui list">
            <div class="item">
                <button type="submit" name="provider" value="OpenIdConnect" class="left floated huge ui button social">
                    <i class="windows icon"></i>
                    <span>My Org Name</span>
                </button>
            </div>
        </div>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

第五ExternalLogin动作不需要改变——我们只是让 OWIN 中间件将我们重定向到外部登录页面。然后流程会将用户引导回ExternalLoginCallback操作。

最后,在ExternalLoginCallback操作中,我检查传入的声明以确定登录是通过 Azure AD 进行的,而不是调用 ASP.NET Identity,我构建了自己的ClaimsIdentity,其中包含我的应用程序识别的所有(特定于应用程序的)声明信息作为管理员用户。

现在,管理员用户导航到https://example.com/admin,单击登录按钮,重定向到 Azure AD 登录名,并以管理员用户身份返回应用程序。