Flatter中的Firebase身份验证和API访问

Flatter中的Firebase身份验证和API访问,firebase,firebase-authentication,flutter,access-token,Firebase,Firebase Authentication,Flutter,Access Token,目前,我有以下几件事: 使用Asp.NETCore2.1编写的API服务器 使用flifter编写的移动应用程序 Firebase身份验证,用于验证用户身份(允许电子邮件、Gmail和Facebook登录) 情况是这样的,我想保护API,只有经过授权的用户才能调用API服务,所以我必须配置API服务器在startup.cs中添加授权,如下所示: public void ConfigureServices(IServiceCollection services) {

目前,我有以下几件事:

  • 使用Asp.NETCore2.1编写的API服务器
  • 使用flifter编写的移动应用程序
  • Firebase身份验证,用于验证用户身份(允许电子邮件、Gmail和Facebook登录)
  • 情况是这样的,我想保护API,只有经过授权的用户才能调用API服务,所以我必须配置API服务器在startup.cs中添加授权,如下所示:

        public void ConfigureServices(IServiceCollection services)
        {
             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Authority = "https://securetoken.google.com/xxxxxxx";
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidIssuer = "https://securetoken.google.com/xxxxxx",
                        ValidateAudience = true,
                        ValidAudience = "xxxxxx",
                        ValidateLifetime = true
                    };
                });
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
             app.UseAuthentication();
        }
    
    之后,在控制器中使用[Authorize]属性,只允许授权用户调用API。这就是我的API配置部分的全部内容

    在移动应用程序客户端中,我正在使用和软件包执行google、facebook登录

    假设我与Facebook提供商一起使用Firebase Auth,我的代码如下:

        final FirebaseAuth _auth = FirebaseAuth.instance;
        final GoogleSignIn _googleSignIn = new GoogleSignIn();
        final FacebookLogin facebookSignIn = new FacebookLogin();
    
        Future _testFacebookLogin() async {
            final FacebookLoginResult result = await facebookSignIn.logInWithReadPermissions(['email', 'public_profile']);
    
            switch (result.status) {
              case FacebookLoginStatus.loggedIn:
                final FacebookAccessToken accessToken = result.accessToken;
                final FirebaseUser user = await _auth.signInWithFacebook(accessToken: accessToken.token);
                assert(user.email != null);
                final FirebaseUser currentUser = await _auth.currentUser();
                assert(user.uid == currentUser.uid);
                //Perform API call here?
                break;
              case FacebookLoginStatus.cancelledByUser:
    
                break;
              case FacebookLoginStatus.error:
    
                break;
            }
        }
    
    我可以登录并从Facebook获得访问令牌。我将访问令牌传递给Firebase Auth,用户可以在Firebase下注册

    我的问题

  • Firebase是否会返回访问令牌和刷新令牌?我找不到存储在currentUser变量中的任何访问令牌
  • 在用户身份验证之后,我应该向授权头中注入什么访问令牌(Facebook或Firebase),以便调用我的API

  • firebase确实返回令牌,请在此处选中此项,您应该将firebase访问令牌放入授权标头中