首页 文章

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

提问于
浏览
13

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

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

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

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

EDIT:

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

3 回答

  • 4

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

    First ,添加对 Microsoft.Owin.Security.OpenIdConnect Nuget包的引用 .

    Second ,在我的_2681637中进行配置:

    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"
        }
    });
    

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

    Azure Active Directory Application Configuration

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

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

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

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

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

  • 0

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

  • 0

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

    如果您的应用程序' signin experience involves the user clicking on 2681651 stickers - you can add 2681652 (there'甚至是推荐的品牌风格:http://msdn.microsoft.com/en-us/library/azure/dn132598.aspx如果您的应用程序执行领域发现并将用户转发到相应的IdP(使用类似"Enter your email address to sign in"的文本框) - 那么您可以为您的公司名称电子邮件地址添加匹配并转发那些用户到AAD .

    在这两种情况下,您的应用程序都会向AAD的SSO endpoints 发出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

相关问题