我可以一起使用WsFederationAuthentication和社交登录提供商吗?

时间:2016-10-06 00:01:02

标签: asp.net oauth-2.0 owin adfs

我的应用程序有两个区域:使用ADFS身份验证的管理区域和使用社交提供程序身份验证的公共区域(Google,Facebook等)。

ADFS和社交登录提供程序各自单独工作,但当我尝试同时使用它们时,它们会相互分开。

这是我的Startup.cs代码:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
            // I have tried setting this to DefaultAuthenticationTypes.ExternalCookie
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            // SECTION A: These lines work when SECTION B (below) is commented out.
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            UseGoogleAuthentication(app);

            // SECTION B: These lines work when SECTION A (above) is commented out.
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            UseWsFederationAuthentication(app);

            // I have tried switching SECTION A and SECTION B
    }

    private static void UseGoogleAuthentication(IAppBuilder app)
    {
        var googleClientId = ConfigurationManager.AppSettings["GoogleClientID"];
        var googleClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"];

        app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
        {
            ClientId = googleClientId,
            ClientSecret = googleClientSecret
        });
    }

    private static void UseWsFederationAuthentication(IAppBuilder app)
    {
        var metaDataAddress = ConfigurationManager.AppSettings["ida:ADFSMetadata"];
        var realm = ConfigurationManager.AppSettings["ida:Wtrealm"];

        app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
        {
            MetadataAddress = metaDataAddress,
            Wtrealm = realm
        });
    }
}

观察到的行为

ADFS取消注释

转到管理区域

  • 用户登录ADFS并重定向回管理区域。
  • .AspNet.Cookies Cookie存在。
  • Context.GetOwinContext().Authentication.User有ADFS声明。

社会未注释

转到公共区域

  • 用户登录Google并重定向回公共区域。
  • .AspNet.ExternalCookie存在。
  • 我构造一个ClaimsIdentity并将其存储在FedAuth cookie中。

两者都没有注释

管理区域优先,公共区域第二

  • 转到管理区域

    • 用户登录ADFS并重定向回管理区域。
    • .AspNet.Cookies Cookie NOT 在场。
    • .AspNet.ExternalCookies Cookie IS 目前。
    • ADFS正在发回正确的声明,但Context.GetOwinContext().Authentication.User
    • 中不再出现这些声明
  • 前往公共区域

    • .AspNet.ExternalCookies Cookie与 SAME 值一起出现。
    • 使用Google登录。
    • .AspNet.ExternalCookies Cookie存在,但 不同 值。

公共区域优先,管理区域第二

  • 去公共场所
    • 用户登录Google并重定向回公共区域。
    • .AspNet.ExternalCookies Cookie存在。
  • 转到管理区域
    • 使用ADFS登录。
    • 声明是从ADFS发回的,但不再出现在Context.GetOwinContext().Authentication.User

1 个答案:

答案 0 :(得分:1)

这是最终的工作代码:

最终,需要有两个不同的cookie - 一个用于ADFS,一个用于外部提供商。 UseWsFederationAuthenticationUseExternalSignInCookie每个都假定会导致另一方出现问题。

具体来说,app.UseExternalSignInCookie设置默认的身份验证类型(请参阅下面的来源)。

public static void UseExternalSignInCookie(this IAppBuilder app, string externalAuthenticationType)
{
    if (app == null)
        throw new ArgumentNullException("app");
    app.SetDefaultSignInAsAuthenticationType(externalAuthenticationType);
    IAppBuilder app1 = app;
    CookieAuthenticationOptions authenticationOptions = new CookieAuthenticationOptions();
    authenticationOptions.AuthenticationType = externalAuthenticationType;
    authenticationOptions.AuthenticationMode = AuthenticationMode.Passive;
    authenticationOptions.CookieName = ".AspNet." + externalAuthenticationType;
    authenticationOptions.ExpireTimeSpan = TimeSpan.FromMinutes(5.0);
    CookieAuthenticationOptions options = authenticationOptions;
    app1.UseCookieAuthentication(options);
}

请注意,它会调用app.SetDefaultSignInAsAuthenticationType(externalAuthenticationType)

如果您未设置WsFederationAuthenticationOptions.TokenValidationParameters.AuthenticationTypeUseWsFederationAuthentication将使用默认身份验证类型(即app.UseExternalSignInCookie设置的身份验证类型)。

我猜测OWIN在创建,序列化和反序列化身份验证Cookie时使用AuthenticationType作为密钥。由于ADFS和外部提供程序使用相同的AuthenticationType(即ExternalCookie),OWIN无法确定使用哪一个并覆盖WsFederation cookie。

还有其他一些小细节,但这是主要细节。

相关问题