我正在使用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:签名验证失败。无法匹配密钥: