繁体   English   中英

获取社交登录提供商返回的流量,以在我的.net core 2.2 Web应用程序上创建本地帐户

[英]Get traffic returned from social login providers to create a local account on my .net core 2.2 web app

我总是使用家庭烘焙和第三方套餐的组合来完成社交登录(特别是Google,Facebook和Twitter)。 在我完全控制的情况下,我可以指定返回方法,然后从社交提供程序捕获响应数据,将我想要的字段(名称,电子邮件地址和第三方ID)映射到我自己的本地模型以创建本地用户帐户然后指定哪种类型的帐户(管理员,普通用户等)。

使用.net核心2.2,一切似乎都发生在幕后,我无法弄清楚如何在调用/例如在facebook中回来之后捕获流量。 幕后调用返回一个JSON响应,然后我的本地应用程序映射到声明以登录我。这一切都很棒,但我也想捕获这些数据并在我自己的数据存储中创建一个本地用户帐户。

我可以在小提琴手中看到这一切发生,所以我知道它仍然在做工作,我只是不知道如何利用它来提取我在该过程中的那个阶段需要的东西。 在我的例子中坚持使用facebook,我想我可以在startup.cs文件中更改facebookOptions.CallbackPath属性,然后在facebook应用程序中将该回调添加为接受的回调,在与该路径匹配的控制器上连接一个方法从那里获取数据。 我可以手动完成剩下的工作,但所有这一切都是将.net核心正在进行的幕后工作转移到那个“位置”。

我想我每次用户点击网站上的页面进行身份验证时都可以进行检查,看看我是否需要添加或更新本地会话,但这对我来说似乎过于无聊。 我希望能够在每次登录后立即执行此操作。我将是第一个承认我的身份技能非常弱的人,所以也许我在那里缺少一些东西?

任何指导或建议将不胜感激。

TIA

我找到了一种方法来做到这一点,但我不确定这是否是最好的方法。我创建了一个带有静态方法的辅助类,我在AddAuthentication()里面的Options.Events.OnCreatingTicket事件中调用startup.cs。

从这里开始,我可以遍历我的声明列表,将用户保存到我的本地数据存储区,并创建或更新本地用户信息。

如果这不是解决这个问题的最好办法,我会喜欢一些更好的方向。

TIA

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
                options =>
                {
                    options.LoginPath = new PathString("/account");
                })
            .AddFacebook(facebookOptions =>
                {
                    facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
                    facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
                    facebookOptions.CallbackPath = new PathString("/account/facebook");
                    facebookOptions.Events.OnCreatingTicket = ctx =>
                    {
                        ExternalLoginProviderHelper.LoginLocally(ctx.Principal);
                        return Task.CompletedTask;
                    };
                })
            .AddGoogle(options =>
                {
                    options.ClientId = Configuration["Authentication:Google:ClientId"];
                    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                    options.Events.OnCreatingTicket = ctx =>
                    {
                        ExternalLoginProviderHelper.LoginLocally(ctx.Principal);
                        return Task.CompletedTask;
                    };
        });

和我的助手班:

public class ExternalLoginProviderHelper
{
    public static void LoginLocally(ClaimsPrincipal claimsPrincipal)
    {
        foreach(Claim claim in claimsPrincipal.Claims)
        {
            // extract the claim information here (ID, Email address, name (surname, given name) etc)
            // make repository call to save information to the datastore and get a local user ID
        }
        // also set other claims for local user id, user type (admin, regular user) etc.
        ClaimsIdentity claimsIdentity = (ClaimsIdentity)claimsPrincipal.Identity;
        claimsIdentity.AddClaim(new Claim("usertype", "admin")); // this is just an example N/V pair
    }
}

暂无
暂无

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

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