混合使用ASP.net Identity和Azure AD身份验证

时间:2019-10-08 16:17:58

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

我们目前将ASP.Net Identity用于数据库支持的登录,并且需要添加对Azure AD SSO的支持。

我非常感谢,一旦登录,我将需要将SSO用户链接到我们系统中的用户以分配相关的声明和角色,但是正在努力使2种身份验证方法和app并用。UseCookieAuthentication似乎在我问题的根源。

当前,我们有:

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Login/Index"),
            ReturnUrlParameter = "url",
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, IdentityUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            ExpireTimeSpan = TimeSpan.FromMinutes(double.Parse(ConfigurationManager.AppSettings["AuthenticationTimeout"])),
            SlidingExpiration = true
        });

有了这个,我怀疑SSO返回的cookie不能正确处理,因为Request.IsAuthenticated始终为false。

如果我将其更改为:

app.UseCookieAuthentication(new CookieAuthenticationOptions());

然后,SSO工作并向我返回经​​过身份验证的请求,但显然会破坏身份登录。

有关信息,我的OpenId设置如下,目前只是试图使其与我们的工作广告一起使用,但最终需要扩展为多租户:

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                PostLogoutRedirectUri = redirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                ResponseType = OpenIdConnectResponseType.IdToken,
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false
                },
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            }
        );

感谢任何帮助或指点。

0 个答案:

没有答案