Токен аутентификации Firebase недействителен - PullRequest
0 голосов
/ 19 января 2019

Я использую формы Xamarin и получаю токен Firebase, используя этот код.

using System;
using Android.Content;
using Android.Gms.Auth.Api;
using Android.Gms.Auth.Api.SignIn;
using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.OS;
using GoogleNativeLogin.Models;
using GoogleNativeLogin.Services.Contracts;
using Plugin.CurrentActivity;

namespace GoogleNativeLogin.Droid
{
    public class GoogleManager : Java.Lang.Object, IGoogleManager, GoogleApiClient.IConnectionCallbacks, GoogleApiClient.IOnConnectionFailedListener
    {
        public Action<GoogleUser, string> _onLoginComplete;
        public static GoogleApiClient _googleApiClient { get; set; }
        public static GoogleManager Instance { get; private set; }

        private const string webClientId = "********************************.apps.googleusercontent.com";

        public GoogleManager()
        {
            Instance = this;
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                                             .RequestIdToken(webClientId)
                                                             .RequestEmail()
                                                             .Build();

            _googleApiClient = new GoogleApiClient.Builder(CrossCurrentActivity.Current.AppContext)
                .AddConnectionCallbacks(this)
                .AddOnConnectionFailedListener(this)
                .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .AddScope(new Scope(Scopes.Profile))
                .Build();
        }

        public void Login(Action<GoogleUser, string> onLoginComplete)
        {
            _onLoginComplete = onLoginComplete;
            Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(_googleApiClient);
            CrossCurrentActivity.Current.Activity.StartActivityForResult(signInIntent, 1);
            _googleApiClient.Connect();
        }

        public void Logout()
        {
            _googleApiClient.Disconnect();
        }

        public void OnAuthCompleted(GoogleSignInResult result)
        {
            if (result.IsSuccess)
            {
                GoogleSignInAccount accountt = result.SignInAccount;
                _onLoginComplete?.Invoke(new GoogleUser()
                {
                    Token = accountt.IdToken,
                    Name = accountt.DisplayName,
                    Email = accountt.Email,
                    Picture = new Uri((accountt.PhotoUrl != null ? $"{accountt.PhotoUrl}" : $"https://autisticdating.net/imgs/profile-placeholder.jpg"))
                }, string.Empty);
            }
            else
            {
                _onLoginComplete?.Invoke(null, "An error occured!");
            }
        }

        public void OnConnected(Bundle connectionHint)
        {

        }

        public void OnConnectionSuspended(int cause)
        {
            _onLoginComplete?.Invoke(null, "Canceled!");
        }

        public void OnConnectionFailed(ConnectionResult result)
        {
            _onLoginComplete?.Invoke(null, result.ErrorMessage);
        }
    }
}

И я тестирую токен локально, используя Postman, для моего локального основного веб-API Asp.Net. Код аутентификации такой.

public static IServiceCollection AddFirebaseAuthentication(this IServiceCollection services, string issuer, string audience)
        {
            var configurationManager = new ConfigurationManager<OpenIdConnectConfiguration>($"{issuer}/.well-known/openid-configuration", new OpenIdConnectConfigurationRetriever());
            services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.IncludeErrorDetails = true;
                o.RefreshOnIssuerKeyNotFound = true;
                o.MetadataAddress = $"{issuer}/.well-known/openid-configuration";
                o.ConfigurationManager = configurationManager;
                o.Audience = audience;
            });
            return services;
        }

        public static IServiceCollection AddFirebaseAuthentication(this IServiceCollection services, string firebaseProject)
        {
            return services.AddFirebaseAuthentication("https://securetoken.google.com/" + firebaseProject, firebaseProject);
        }

Я передаю идентификатор проекта Firebase этой функции.

 IServiceCollection AddFirebaseAuthentication(this IServiceCollection services, string firebaseProject)

И вызов API с атрибутом Authorize. И я получаю эту ошибку в Визуальная студия вывода.

Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException: IDX10501: Ошибка проверки подписи. Невозможно сопоставить ключи:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...