繁体   English   中英

.Net Core 2.1 CORS和Firebase JWT授权

[英].Net Core 2.1 CORS and Authorization with Firebase JWT

我正在关注此博客文章,内容涉及使用.net Core 2通过Firebase进行身份验证https://blog.markvincze.com/secure-an-asp-net-core-api-with-firebase/

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

我正在使用带有.net核心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
  }
})

启动文件

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");
    }
}

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

任何帮助都感激不尽!

谢谢!

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

您可以使用此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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM