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

标签 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 或任何其他外部提供商进行身份验证。我发现的最接近的问题是这个:How to do both Azure Active Directory Single Sign On and Forms Authentications on ASP.NET MVC

我查看了使用 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);
                    }
                }
            }
        );
    }  

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

AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,

或者,只需设置不带任何值的 CookieAuthentication。

app.UseCookieAuthentication(new CookieAuthenticationOptions { });

我猜想有一个相对简单的方法可以实现这一点,并且希望能提供一些关于从哪里开始寻找的想法。

最佳答案

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

  1. WebApp-WSFederation-DotNet
  2. WebApp-MultiTenant-OpenIdConnect-DotNet
  3. WebApp-OpenIDConnect-DotNet

另一个例子是hereWindowsAzureActiveDirectoryBearerAuthenticationOptions

关于c# - 使用密码和 Azure Active Directory 身份验证的 Asp.net Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660780/

相关文章:

c# - 使用事务的批量插入会阻止异步进程上的 UI

swift - 在 Swift 中进行身份验证的 Azure Api 应用

c# - ADAL.NET v3 不支持 AcquireToken 和 UserCredential?

c# - 如何使乘法运算符(*)表现为短路?

c# - 查询 EDMX 中的多对多联接表

c# - 数组被C#虚拟机写保护

asp.net-mvc - 当模型具有多个子项时,如何分层组织 View 和 Controller

asp.net-mvc - 我可以动态更改 asp.net 中的 FormsAuthentication cookie 名称吗?

c# - 如何使用 Razor 重定向到主页

azure - 从具有动态外部 IP 的计算机连接到 Azure SQL Server