【问题标题】:Firebase authentication asp.net coreFirebase 身份验证 asp.net 核心
【发布时间】:2017-01-04 13:20:19
【问题描述】:

成功登录 Firebase 后,我们收到了一个 JWT 令牌。

为了向我的 asp.net 应用程序添加授权,我尝试将 JwtBearerAuthentication 添加到我的中间件。

我尝试了以下 JwtBearerOptions:

 var options = new JwtBearerOptions
        {
            Audience = "myApp",
            Authority = "https://securetoken.google.com"
        };

 var options = new JwtBearerOptions
        {
            Audience = "myApp",
            Authority = "https://securetoken.google.com/myApp"
        };

不幸的是,这不起作用。我的授权 URL 可能不正确。

有人知道哪个权威网址是正确的吗?

【问题讨论】:

    标签: c# firebase asp.net-core jwt firebase-authentication


    【解决方案1】:

    Firebase 在此处发布标准格式的 JWK:

    https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com

    (尽管他们在文档中没有提及)

    我在这里找到了这个信息:https://github.com/cfworker/cfworker/issues/89#issuecomment-748422827

    【讨论】:

    【解决方案2】:

    JWT 验证需要手动:source

    以下代码正在验证 FirebaseToken (JWT):

        //Download certificates from google
        HttpClient client = new HttpClient();
        var jsonResult = client.GetStringAsync("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com").Result;
    
        //Convert JSON Result
        var x509Metadata = JObject.Parse(jsonResult)
                            .Children()
                            .Cast<JProperty>()
                            .Select(i => new x509Metadata(i.Path, i.Value.ToString()));
    
        //Extract IssuerSigningKeys
        var issuerSigningKeys = x509Metadata.Select(s => s.X509SecurityKey);
    
        //Setup JwtTokenHandler 
        var handler = new JwtSecurityTokenHandler();
        SecurityToken token;
        handler.ValidateToken(user.FirebaseToken, new TokenValidationParameters
        {
            IssuerSigningKeys = issuerSigningKeys,
            ValidAudience = "myApp",
            ValidIssuer = "https://securetoken.google.com/myApp",
            IssuerSigningKeyResolver = (arbitrarily, declaring, these, parameters) => issuerSigningKeys
        }, out token);
    
    public class x509Metadata
    {
        public string KID { get; set; }
        public string Certificate { get; set; }
        public X509SecurityKey X509SecurityKey { get; set; }
    
        public x509Metadata(string kid, string certificate)
        {
            KID = kid;
            Certificate = certificate;
            X509SecurityKey = BuildSecurityKey(Certificate);
        }
    
        private X509SecurityKey BuildSecurityKey(string certificate)
        {
            //Remove : -----BEGIN CERTIFICATE----- & -----END CERTIFICATE-----
            var lines = certificate.Split('\n');
            var selectedLines = lines.Skip(1).Take(lines.Length - 3);
            var key = string.Join(Environment.NewLine, selectedLines);
    
            return new X509SecurityKey(new X509Certificate2(Convert.FromBase64String(key)));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2021-06-26
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2021-06-11
      • 2017-09-13
      • 2019-11-03
      • 2019-02-23
      相关资源
      最近更新 更多