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

时间:2014-05-21 12:11:36

标签: authentication azure asp.net-identity asp.net-mvc-5.1 azure-active-directory

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

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

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

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

修改

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

3 个答案:

答案 0 :(得分:4)

我设法通过执行以下操作来实现此目的:

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

第二次,在我的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"
    }
});

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

Azure Active Directory Application Configuration

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

@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>
}

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

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

现在,管理员用户导航到https://example.com/admin,单击登录按钮,重定向到Azure AD登录,然后以管理员用户的形式回退到应用程序。

答案 1 :(得分:0)

您最好的选择是利用Azure AD访问控制服务(ACS)并设置身份提供商以包含Azure AD,Facebook等。请参阅此处的文档:http://azure.microsoft.com/en-us/documentation/articles/active-directory-dotnet-how-to-use-access-control/

答案 2 :(得分:0)

确实可以使用ACS,但是由于您已经实施了Google / Facebook登录,我建议您直接与Azure AD集成,而不是通过像ACS / thinktecture这样的中间STS。

如果你的应用程序&#39;登录体验涉及用户点击&#34;使用Google登录Google /登录&#34;贴纸 - 您可以添加&#34;与您的公司签约&#39; 。帐户&#34; (甚至推荐的品牌风格:http://msdn.microsoft.com/en-us/library/azure/dn132598.aspx 如果您的应用程序执行领域发现并将用户转发到相应的IdP(使用类似&#34的文本框;输入您的电子邮件地址以登录&#34;) - 那么您可以为您的公司名称电子邮件地址和将这些用户转发给AAD。

在这两种情况下,您的应用程序都会向AAD的SSO端点发出SSO请求:https://login.windows.net/ {company domain name / id} / {wsfed / saml / oauth2}。如果你正在使用.Net,WSFed,这应该会看到你:http://msdn.microsoft.com/en-us/library/azure/dn151789.aspx。寻找代码:

SignInRequestMessage sirm = FederatedAuthentication.WSFederationAuthenticationModule.CreateSignInRequest("", HttpContext.Request.RawUrl, false);
result = Redirect(sirm.RequestUrl.ToString());   

这里还有一个OpenIdConnect示例:http://msdn.microsoft.com/en-us/library/azure/dn151789.aspx