FirebaseAuthenticationasp.netCore成功登录Firebase后,我们收到了一个JWTtoken。为了向我的asp.net应用程序添加授权,我试图将JwtBearerAuthentication添加到我的中间件。我尝试了以下JwtBearerOptions:varoptions=newJwtBearerOptions{Audience="myApp",Authority="https://securetoken.google.com"};和varoptions=newJwtBearerOptions{Audience="myApp",Authority="https://securetoken.google.com/myApp"};不幸的是,这不起作用。我的Auhtorityurl可能不正确。有谁知道Auhtorityurl是否正确?JWT验证需要手动完成:下面源码是validationFirebaseToken(JWT):以上是C#学习教程:Firebaseauthenticationasp.netcore分享,如果对大家有用还需要详细了解C#学习教程,希望大家多多关注——//从google下载证书HttpClientclient=newHttpClient();varjsonResult=client.GetStringAsync(“https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com”)。结果;//转换JSON结果varx509Metadata=JObject.Parse(jsonResult).Children().Cast().Select(i=>newx509Metadata(i.Path,i.Value.ToString()));//提取IssuerSigningKeysvarissuerSigningKeys=x509Metadata.Select(s=>s.X509SecurityKey);//设置JwtTokenHandlervarhandler=newJwtSecurityTokenHandler();SecurityToken令牌;handler.ValidateToken(user.FirebaseToken,newTokenValidationParameters{IssuerSigningKeys=issuerSigningKeys,ValidAudience="myApp",ValidIssuer="https://securetoken.google.com/myApp",IssuerSigningKeyResolver=(任意,声明ng,these,parameters)=>issuerSigningKeys},outtoken);publicclassx509Metadata{publicstringKID{get;放;}公共字符串证书{得到;放;}publicX509SecurityKeyX509SecurityKey{得到;放;}publicx509Metadata(stringkid,stringcertificate){KID=kid;证书=证书;X509SecurityKey=BuildSecurityKey(证书);}privateX509SecurityKeyBuildSecurityKey(stringcertificate){//删除:-----BEGINCERTIFICATE-----&-----ENDCERTIFICATE-----varlines=certificate.Split('n');varselectedLines=lines.Skip(1).Take(lines.Length-3);varkey=string.Join(Environment.NewLine,selectedLines);返回新的X509SecurityKey(新的X509Certificate2(Convert.FromBase64String(key)));}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
