xamarin.forms - Firebase 身份验证令牌无效
问题描述
我正在使用 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 在本地测试令牌到我的本地 Asp.Net 核心 Web API。验证码是这个。
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 项目 ID 传递给此函数。
IServiceCollection AddFirebaseAuthentication(this IServiceCollection services, string firebaseProject)
并使用 Authorize 属性调用 API。我在 Visual Studio 输出中收到此错误。
Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException:IDX10501:签名验证失败。无法匹配键:
解决方案
推荐阅读
- java - 在 HashMap 中加载文件时出现 Java 堆内存问题
- .htaccess - .htaccess 相同单词的重写规则
- angular - 使用 Router Scroller 平滑滚动
- vba - 将具有相同文件名(“前缀”+日期)的多个文件中的最新文件移至文件夹
- apache-spark - Pyspark 的 repartition() (Dataframe API) 的奇怪行为
- android - Android/OkHttp3,基本身份验证方法的问题
- airflow - 我无法使用 Apache 气流发送失败的电子邮件
- arrays - 使用速记从数组中打印数据
- c# - 如何使用 LINQ 从子集合中选择所有项目
- php - 在 header.php 文件的 php 命令中添加 Google Ads 事件片段