Cookies 为什么用户被重定向到登录屏幕?

Cookies 为什么用户被重定向到登录屏幕?,cookies,asp.net-core,.net-core,identityserver4,openid-connect,Cookies,Asp.net Core,.net Core,Identityserver4,Openid Connect,我想知道为什么我的客户端被重定向到identity server 我采取以下步骤: 重定向到identity server以进行登录 登录并重定向回客户端 使用应用程序 停止使用它 12个多小时后打开应用程序 重定向到登录屏幕 搔我的头 此时,identity server、客户端和api被部署到共享主机提供商,我正在使用EF存储配置和操作 身份服务器配置 services.AddIdentityServer(选项=> { options.Authentication.CookieSliding

我想知道为什么我的客户端被重定向到identity server

我采取以下步骤:

  • 重定向到identity server以进行登录
  • 登录并重定向回客户端
  • 使用应用程序
  • 停止使用它
  • 12个多小时后打开应用程序
  • 重定向到登录屏幕
  • 搔我的头
  • 此时,identity server、客户端和api被部署到共享主机提供商,我正在使用EF存储配置和操作

    身份服务器配置

    services.AddIdentityServer(选项=>
    {
    options.Authentication.CookieSlidingExpiration=true;
    options.Authentication.CookieLifetime=TimeSpan.FromDays(1);
    }) 
    .AddDeveloperSigningCredential()
    .AddAsNetIdentity()
    .AddConfigurationStore(选项=>
    {
    options.ConfigureDbContext=builder=>
    builder.UseSqlServer(connectionString,
    sql=>sql.migrationassembly(migrationassembly));
    })
    .addStore(选项=>
    {
    options.ConfigureDbContext=builder=>
    builder.UseSqlServer(connectionString,
    sql=>sql.migrationassembly(migrationassembly));
    options.EnableTokenCleanup=true;
    options.TokenCleanupInterval=30;
    });
    服务。添加身份验证(“自定义”)
    .AddCookie(“自定义”,选项=>
    {
    options.Cookie.Name=“自定义”;
    options.ExpireTimeSpan=TimeSpan.FromDays(1);
    });
    services.AddAuthentication()
    .AddFacebook(选项=>
    {
    options.AppId=“1”;
    options.AppSecret=“2”;
    });
    
    Config.cs中的客户端

    新客户端
    {
    ClientId=“mvc客户端”,
    ClientName=“Mvc客户端”,
    AllowedGrantTypes=GrantTypes.HybridAndClientCredentials,
    RequireSent=false,
    客户秘密=
    {
    新密码(“Secret.Sha256())
    },
    重定向URI={$“{address}符号oidc},
    PostLogoutRedirectUris={$“{address}客户端/“},
    AllowedScopes=新列表
    {
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile,
    “api”
    },
    RefreshTokenUsage=TokenUsage.OneTimeOnly,
    RefreshTokenExpiration=令牌过期。滑动,
    SlidingRefreshTokenLifetime=(3600*24*30),
    IdentityTokenLifetime=(60*15),
    AccessTokenLifetime=300,
    AllowOfflineAccess=true,
    }
    
    MVC客户端配置

    services.AddAuthentication(选项=>
    {
    options.DefaultScheme=“Cookies”;
    options.DefaultChallengeScheme=“oidc”;
    })
    .AddCookie(“Cookies”,选项=>
    {
    options.Cookie.Expiration=TimeSpan.FromDays(30);
    })
    .AddOpenIdConnect(“oidc”,选项=>
    {
    options.signnscheme=“Cookies”;
    options.Authority=$“{u config[“Server”]}”;
    options.RequireHttpsMetadata=false;
    options.ClientId=“mvc客户端”;
    options.ClientSecret=“secret”;
    options.ResponseType=“代码id\U令牌”;
    选项。范围。添加(“api”);
    options.Scope.Add(“脱机访问”);
    options.GetClaimsFromUserInfoEndpoint=true;
    options.SaveTokens=true;
    options.TokenValidationParameters=新的TokenValidationParameters
    {
    NameClaimType=JwtClaimTypes.Name,
    RoleClaimType=JwtClaimTypes.Role,
    };
    });
    
    令牌续订服务筛选器

    var accessToken=await context.HttpContext.GetTokenAsync(“访问令牌”);
    var introspectionClient=新的introspectionClient(_-ipoint,“api”,“api-secret”);
    var response=await introspectionClient.sendsync(新的IntrospectionRequest{Token=accessToken});
    如果(!response.IsActive)
    {
    var issuer=_config[“服务器”].ToLower();
    var客户=新发现客户(发行人);
    client.Policy.RequireHttps=false;
    var disco=await client.GetAsync();
    if(disco.IsError)抛出新异常(disco.Error);
    var tokenClient=新的tokenClient(disco.TokenEndpoint,“mvc客户端”,“机密”);
    var rt=await context.HttpContext.GetTokenAsync(“刷新令牌”);
    var tokenResult=wait tokenClient.RequestRefreshTokenAsync(rt);
    如果(!tokenResult.IsError)
    {
    var info=await context.HttpContext.authenticateSync(“Cookies”);
    var old_id_token=wait context.HttpContext.GetTokenAsync(“id_token”);
    var new_access_token=tokenResult.AccessToken;
    var new_refresh_token=tokenResult.refreshttoken;
    var tokens=新列表();
    添加(新身份验证令牌{Name=OpenIdConnectParameterNames.IdToken,Value=old_id_token});
    添加(新的AuthenticationToken{Name=OpenIdConnectParameterNames.AccessToken,Value=new\u access\u token});
    添加(新的AuthenticationToken{Name=OpenIdConnectParameterNames.RefreshToken,Value=new\u refresh\u token});
    var expiresAt=DateTime.UtcNow+TimeSpan.FromSeconds(tokenResult.ExpiresIn);
    添加(新身份验证令牌{Name=“expires\u at”,Value=expiresAt.ToString(“o”,CultureInfo.InvariantCulture)});
    信息。属性。存储令牌(令牌);
    wait context.HttpContext.SignInAsync(“Cookies”,info.Principal,info.Properties);
    }
    其他的
    {
    wait context.HttpContext.SignOutAsync(“Cookies”);
    }
    }
    等待下一个();
    
    注意:我试图在IS上覆盖的cookie中间件不起作用。(示例:登录后,我无法在开发人员工具中看到“自定义”cookie)

    所以我的逻辑是刷新令牌是
    services.AddDataProtection()
        .SetApplicationName("Server")
        .PersistKeysToFileSystem(new DirectoryInfo(_config["MachineKeys"]));