簡體   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