我的应用程序有两个区域:使用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
});
}
}
转到管理区域
.AspNet.Cookies
Cookie存在。Context.GetOwinContext().Authentication.User
有ADFS声明。转到公共区域
.AspNet.ExternalCookie
存在。管理区域优先,公共区域第二
转到管理区域
.AspNet.Cookies
Cookie NOT 在场。.AspNet.ExternalCookies
Cookie IS 目前。Context.GetOwinContext().Authentication.User
前往公共区域
.AspNet.ExternalCookies
Cookie与 SAME 值一起出现。.AspNet.ExternalCookies
Cookie存在,但 不同 值。公共区域优先,管理区域第二
.AspNet.ExternalCookies
Cookie存在。Context.GetOwinContext().Authentication.User
。答案 0 :(得分:1)
这是最终的工作代码:
最终,需要有两个不同的cookie - 一个用于ADFS,一个用于外部提供商。 UseWsFederationAuthentication
和UseExternalSignInCookie
每个都假定会导致另一方出现问题。
具体来说,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.AuthenticationType
,UseWsFederationAuthentication
将使用默认身份验证类型(即app.UseExternalSignInCookie
设置的身份验证类型)。
我猜测OWIN在创建,序列化和反序列化身份验证Cookie时使用AuthenticationType作为密钥。由于ADFS和外部提供程序使用相同的AuthenticationType(即ExternalCookie),OWIN无法确定使用哪一个并覆盖WsFederation cookie。
还有其他一些小细节,但这是主要细节。