【问题标题】:.Net Core 2.1 CORS and Authorization with Firebase JWT.Net Core 2.1 CORS 和 Firebase JWT 授权
【发布时间】:2019-03-18 01:10:37
【问题描述】:

我正在关注这篇关于使用 .net Core 2 https://blog.markvincze.com/secure-an-asp-net-core-api-with-firebase/987654321@ 使用 firebase 进行身份验证的博文

(我意识到我正在使用 .net core 2.1,但认为它必须是相似的)

我正在使用带有 .net core 2.1 WebApi 后端的 React 前端。

我可以点击控制器没有问题,但是一旦我尝试将身份验证添加到 startup.cs,我就会得到: 跨域请求被阻止:同源策略不允许读取 localhost:4000 处的远程资源(原因:CORS 请求未成功)

在那之前一切正常

我的请求来自http://localhost:3000

更新---------------------------------------------- --------------------

附带说明,这在使用 POSTMAN 时有效。我可以通过 Firebase 进行身份验证并毫无问题地点击控制器

也适用于 chrome。好像是firefox浏览器的问题

我的实现(在 Firebase 登录前端成功之后)

Axios 请求

axois
.get("https://localhost:4000/v1/picture", {
  headers: {
    accept: "application/json",
    "Accept-Language": "en-US,en;q=0.8",
    "Content-Type": `multipart/form-data;`,
    Authorization: "Bearer " + localStorage.getItem("token") 
    //Is the above the correct way to pass a jwt to be authenticated backend? This is the full jwt returned by Firebase
  }
})

Startup.cs

services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin",
                    builder => builder.WithOrigins("http://localhost:3000")
                        .AllowAnyMethod()
                        .AllowAnyHeader());
            }
        );

        //https://blog.markvincze.com/secure-an-asp-net-core-api-with-firebase/
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.Authority = "https://securetoken.google.com/mafirebaseapp";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer = "https://securetoken.google.com/mafirebaseapp",
                    ValidateAudience = true,
                    ValidAudience = "mafirebaseapp",
                    ValidateLifetime = true
                };
            });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
...

...
        app.UseCors("AllowSpecificOrigin");
        app.UseAuthentication();
        app.UseHttpsRedirection();
        app.UseMvc();
}

PictureController.cs

[Route("v1/picture")]
public class PictureController : Controller
{
    [Authorize]
    [HttpGet]
    public IActionResult GetPicture()
    {
        return Ok("Hi");
    }
}

我查看了另一篇文章,其中指出方法的顺序有所不同,所以我认为这不是问题。

任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 我认为标题名称和值都需要用引号引起来,例如"accept": "application/json" (单引号或双引号 - 没关系)。另外,请确保您使用正确的引号。我会为所有这些使用相同的双引号以保持一致性
  • 我没想到。我会试一试。话虽如此,我正在使用 vscode 美化它实际上从这两个字段中删除了引号。感谢您的意见 =)

标签: c# firebase .net-core firebase-authentication asp.net-core-webapi


【解决方案1】:

您可以尝试为特定操作使用指定 CORS 策略,只需将 [EnableCors("AllowSpecificOrigin")] 添加到您的操作即可。

【讨论】:

    【解决方案2】:

    您可以使用这个 NuGet 包来简化它(支持 AspNetCore >= 2.0)

    安装包 AspNetCore.Firebase.Authentication

    在 Startup.cs 文件中

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddFirebaseAuthentication(Configuration["FirebaseAuthentication:Issuer"], Configuration["FirebaseAuthentication:Audience"]);
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
       app.UseAuthentication();
    }
    

    只需在控制器上使用 [Authorize] 属性来强制授权

    来源:https://bitbucket.org/RAPHAEL_BICKEL/aspnetcore.firebase.authentication/src/master/

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 2020-10-29
      • 2016-12-14
      • 2020-07-21
      • 2021-10-05
      • 2019-03-20
      • 2019-01-04
      • 2020-02-11
      • 2018-08-05
      相关资源
      最近更新 更多