Asp.net Identity使用密码和Azure Active Directory身份验证

Chr*_*isW 15 c# asp.net-mvc azure asp.net-identity azure-active-directory

我正在使用Asp.net Identity(OWIN)构建ASP.NET MVC 5网站,并希望支持传统的用户名/密码身份验证以及针对Azure Active Directory的身份验证.此应用程序不需要针对Microsoft ID(Live ID),Facebook,Twitter或任何其他外部提供程序进行身份验证.我找到的最接近的SO问题是:如何在ASP.NET MVC上执行Azure Active Directory单点登录和表单身份验证

我查看了使用"个人用户帐户"选项以及VS 2015中的"工作和学校帐户"选项创建项目时创建的示例.我的身份验证工作正常; 只有当我尝试将它们结合起来时,我才遇到问题.

在我的Startup_Auth.cs文件中,我正在配置OWIN,如下所示:

    public void ConfigureAuth(IAppBuilder app)
    {

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        //app.UseCookieAuthentication(new CookieAuthenticationOptions { });

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
            LoginPath = new PathString("/account/sign-in")
        });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuer = false,
                },
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    SecurityTokenValidated = (context) => 
                    {
                        return Task.FromResult(0);
                    },
                    AuthorizationCodeReceived = (context) =>
                    {
                        return Task.FromResult(0);
                    },
                    AuthenticationFailed = (context) =>
                    {
                        context.OwinContext.Response.Redirect("/Home/Error");
                        context.HandleResponse(); // Suppress the exception
                        return Task.FromResult(0);
                    }
                }
            }
        );
    }  
Run Code Online (Sandbox Code Playgroud)

此配置适用于密码身份验证,但不适用于AAD身份验证.要启用AAD身份验证,我需要注释掉设置AuthenticationType的行

AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Run Code Online (Sandbox Code Playgroud)

或者,只设置没有值的CookieAuthentication.

app.UseCookieAuthentication(new CookieAuthenticationOptions { });
Run Code Online (Sandbox Code Playgroud)

我猜这有一个相对简单的方法,并会欣赏一些关于从哪里开始寻找的想法.

Bac*_*cks 2

我从微软搜索了例子。所有这些看起来都像您的解决方案。看这里:

  1. Web应用程序-WSFederation-DotNet
  2. WebApp-多租户-OpenIdConnect-DotNet
  3. WebApp-OpenIDConnect-DotNet

另一个例子是这里WindowsAzureActiveDirectoryBearerAuthenticationOptions

  • 我很欣赏这些链接,但我以前已经看过所有这些链接。我的问题不是我不知道如何实现 AAD 身份验证。我明白了。但是在同一解决方案中使用标准密码身份验证实现 AAD 就是我的问题所在,并且您的链接都没有显示该场景。 (3认同)