c# - Iframe 不会尊重登录 cookie

标签 c# cookies iframe asp.net-core identityserver4

我有一个非常奇怪的问题,我已经尝试调试了一个多星期了。我不知道问题出在哪里。我希望这里的某个人以前可能遇到过同样的问题,并且能够告诉我问题可能是什么以及如何解决。

我是一个 asp .net core 2.0 应用程序。独立托管时运行良好。

  1. 主 Controller 需要身份验证。
  2. 登录身份服务器 4。(混合授权类型)
  3. 返回到应用程序获取数据并显示它。

现在,当我尝试将此应用程序作为插件添加到主应用程序中时,它不起作用。它实际上循环。插件显示在 iFrame 的主应用程序中。

<iframe src="https://XXXXX" sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox" width="500" height="500"></iframe>

查看日志。我可以看到它被转发到身份服务器日志记录返回到插件视线我可以看到它

OnSignedIn: IsAuthenticated = True

我可以看到插件有一个访问 token 。然后循环开始。它返回到身份服务器再次请求访问,整个过程无休止地继续下去。

我看到的独立运行和作为插件运行的不同之处在于日志中缺少这一行

Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[10]
AuthenticationScheme: Cookies signed in.

我还可以在请求的 header 中看到身份服务器返回 cookie header 并告诉它执行 setcookie 但它从未设置。

为什么在 iframe 中没有设置 cookie?

我尝试过的

 options.Cookie.SameSite = SameSiteMode.Lax;
 options.Cookie.SecurePolicy = CookieSecurePolicy.None;

Content-Security-Policy header 包括身份服务器、插件站点和主要 Web 应用程序站点。

为什么没有设置 cookie?

插件授权码。

 services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";


            })
            .AddCookie("Cookies", options =>
            {
                options.Cookie.SameSite = SameSiteMode.Lax;
                options.Cookie.SecurePolicy = CookieSecurePolicy.None;
                options.SessionStore = new MemoryCacheTicketStore();                   
            })
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";

                options.Authority = Configuration["ServiceSettings:IdentityServerEndpoint"];
                options.RequireHttpsMetadata = false;

                options.ClientId = Configuration["ServiceSettings:ClientId"];
                options.ClientSecret = Configuration["ServiceSettings:secret"];
                options.ResponseType = "code id_token";

                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                options.Scope.Add("profile");
                options.Scope.Add("testapi");

            });
    }

标题

 app.Use(async (ctx, next) =>
        {
            ctx.Response.Headers.Add("Content-Security-Policy", Configuration["DefaultApplicationSettings:ContentSecurityPolicy"]);

            await next();
        });

设置

 "DefaultApplicationSettings": {
"ContentSecurityPolicy": "default-src 'self' plugin webapp; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com/ webapp; font-src 'self' https://fonts.gstatic.com/ webapp; frame-ancestors 'self' webapp"  },

由于公司隐私,网址已更改。

最佳答案

经过大量搜索,我的一位同事在 Identity Server 4 源代码中找到了一条评论

IdentityServerBuilderExtensions.cs

 // we need to disable to allow iframe for authorize requests
 cookie.Cookie.SameSite = AspNetCore.Http.SameSiteMode.None;

我一变

options.Cookie.SameSite = SameSiteMode.Lax;

options.Cookie.SameSite = SameSiteMode.None;

成功了。

Indicates whether the browser should allow the cookie to be attached to same-site requests only (SameSiteMode.Strict) or cross-site requests using safe HTTP methods and same-site requests (SameSiteMode.Lax). When set to SameSiteMode.None, the cookie header value isn't set. Note that Cookie Policy Middleware might overwrite the value that you provide. To support OAuth authentication, the default value is SameSiteMode.Lax. For more information, see OAuth authentication broken due to SameSite cookie policy.

为什么这行得通,我仍然不清楚,但它行得通。

关于c# - Iframe 不会尊重登录 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857281/

相关文章:

c# - 通过 WMI 查询时防止 ComExceptions

c# - 从具有整数属性的 List<T> 返回 List<int>?

c# - 使用 Select(...).Any(...) 时,select 的方法运行了多少次?

asp.net - Cookie 是否有其他方法来记住用户的选择?

javascript - 浏览器扩展(例如 “memonic”)如何将数据与特定用户相关联?

java - http请求在java中下载文件

javascript 权限/访问被拒绝从 ssl iframe 访问父非 ssl 属性

css - IFrame 中缺少样式(Asp.Net 页面)

c# - 使用 iTextSharp 在 PDF 阅读器中启用保存选项

html - youtube视频未加载到我的本地网页中