【问题标题】:Why is Identity generating insecure redirect URIs?为什么 Identity 会生成不安全的重定向 URI?
【发布时间】:2019-03-02 00:00:03
【问题描述】:

使用 Asp.Net Core 2.1.4。该应用程序位于从 https://github.com/jwilder/nginx-proxy docker 映像运行的 Nginx 反向代理后面,其中包括 x-forwarded-* 标头配置。

在我的Startup.Configure 方法中,我做的第一件事是:

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.All
});

然后在 ConfigureServices 中设置外部身份验证:

services.AddAuthentication().AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = Configuration["FacebookAppId"];
    facebookOptions.AppSecret = Configuration["FacebookSecret"];
})
.AddGoogle(googleOptions =>
{
    googleOptions.ClientId = Configuration["GoogleClientId"];
    googleOptions.ClientSecret = Configuration["GoogleClientSecret"];
});

并配置身份:

services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<IdentityDbContext>()
    .AddDefaultTokenProviders();

然后我有一个控制器操作,我的登录表单发布到:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
    // Request a redirect to the external login provider.
    var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { returnUrl });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
    return Challenge(properties, provider);
}

HTTP 适用于整个站点。我可以在 Chrome 开发工具中看到通向“ExternalLogin”的路由的调用是通过 https 进行的,但是应用程序会将我重定向到 Facebook 或 Google,并使用 http 的 redirect_uri。在验证并重定向回 http url 后,我的应用会自动重定向到 https。

如何让 Identity 生成安全的重定向 uri?

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    根据 .net 核心站点中提供的文档:


    如果应用程序部署在代理服务器或负载均衡器后面,一些原始请求信息可能会在请求标头中转发给应用程序。此信息通常包括安全请求方案 (https)、主机和客户端 IP 地址。应用程序不会自动读取这些请求标头来发现和使用原始请求信息。

    该方案用于影响与外部提供商的身份验证流程的链接生成。 丢失安全方案 (https) 会导致应用生成不正确的不安全重定向 URL。


    要解决这个问题,您可以尝试更改 nginx 配置并获取更多信息here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多