繁体   English   中英

如何在 ASP.NET Core MVC 中集成 firebase 认证

[英]How to integrate firebase authentication in ASP.NET Core MVC

我有一个现有的 ASP.NET 核心 MVC 应用程序,我需要在其中添加 firebase 身份验证。 是否可以将firebase web ui集成到我的 ASP.NET 核心 MVC 应用程序中?

为了在 ASP.NET 核心 MVC 应用程序中实现 Firebase 身份验证集成,您应该尝试两个教程。

这是 Arno Waegemans 的第一个教程和 Heather Downing 的第二个教程。

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)));
    }
}

反映:

https://developer.okta.com/blog/2019/04/30/store-data-firebase-aspnet-mvc-csharp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM