我正在使用通过Cookie使用OpenIdConnectAuthentication的Web应用程序。该应用程序基于WebAPI,但是使用OWIN进行身份验证,因为它最初使用的是Forms身份验证,我们正尝试使用IdentityServer 4插入替代品。 当用户首次点击该应用程序时,身份验证过程将按预期进行,并且身份验证cookie会按预期设置。如果然后手动删除了cookie,那么在刷新页面时就不会进行身份验证,因此我们最终会失败(由于Controller构造中对身份的依赖)。从跟踪输出看,OWIN中间件肯定是在管道中的正确位置调用的,所以为什么不进行cookie身份验证超出了我的范围。
调试中间件,我可以看到在执行OpenIdConnectAuthentication中间件之后未设置身份,因此除非进行一些奇怪的缓存,否则我希望已触发身份验证过程。
短暂延迟后,再次刷新页面,然后页面重新进行验证。
有什么想法吗?
这是客户端的Startup.cs代码段:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "cookies",
AuthenticationMode = AuthenticationMode.Active,
SlidingExpiration = false,
CookieHttpOnly = true,
CookieSecure = CookieSecureOption.Never
});
//Implicit Flow
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = identityServerUrl,
AuthenticationMode = AuthenticationMode.Active,
ClientId = appName,
ResponseType = "id_token token",
RedirectUri = returnUrl, // This application
Scope = "openid profile username", // Basic claims plus the custom 'username' claim
UseTokenLifetime = false, // Otherwise cookie lifetime is too short lived
TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "username"
},
SignInAsAuthenticationType =
"cookies", //Same as AuthenticationType for provider above (in this case cookies)
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async ctx =>
{
var userInfoClient = new UserInfoClient($"{identityServerUrl}/connect/userinfo");
var response = await userInfoClient.GetAsync(ctx.ProtocolMessage.AccessToken);
ctx.AuthenticationTicket.Identity.AddClaims(response.Claims);
ctx.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", ctx.ProtocolMessage.AccessToken));
}
}
});
app.UseStageMarker(PipelineStage.Authenticate);