简体   繁体   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

I've always used a mix of home baked plus 3rd party packages to accomplish social logins (Google, Facebook, and Twitter specifically). 我总是使用家庭烘焙和第三方套餐的组合来完成社交登录(特别是Google,Facebook和Twitter)。 With me in full control, I could specify the return method and then capture the response data from the social provider, map the fields I wanted (name, email address, and 3rd party ID) to my own local model to create a local user account and then specify what type of account that is (admin, regular user etc). 在我完全控制的情况下,我可以指定返回方法,然后从社交提供程序捕获响应数据,将我想要的字段(名称,电子邮件地址和第三方ID)映射到我自己的本地模型以创建本地用户帐户然后指定哪种类型的帐户(管理员,普通用户等)。

With .net core 2.2, everything seems to happen behind the scenes and I can't figure out how to capture the traffic when it comes back after the call to /me for example in facebook. 使用.net核心2.2,一切似乎都发生在幕后,我无法弄清楚如何在调用/例如在facebook中回来之后捕获流量。 That behind the scenes call returns a JSON response that my local app is then mapping to the claims to log me in. That's all great, but I also want to capture that data and create a local user account in my own data store. 幕后调用返回一个JSON响应,然后我的本地应用程序映射到声明以登录我。这一切都很棒,但我也想捕获这些数据并在我自己的数据存储中创建一个本地用户帐户。

I can see it all happening in fiddler so I know it's still doing the work, I just don't know how to tap into it to extract what I need at that stage during the process. 我可以在小提琴手中看到这一切发生,所以我知道它仍然在做工作,我只是不知道如何利用它来提取我在该过程中的那个阶段需要的东西。 Sticking with facebook for my example here, I thought I could change the facebookOptions.CallbackPath property in my startup.cs file, then add that callback as an accepted callback in the facebook app, wire up a method on a controller that matches that path and get the data from there. 在我的例子中坚持使用facebook,我想我可以在startup.cs文件中更改facebookOptions.CallbackPath属性,然后在facebook应用程序中将该回调添加为接受的回调,在与该路径匹配的控制器上连接一个方法从那里获取数据。 I'd be ok with having the work out the rest of the process by hand, but all that did was move the behind the scenes work that .net core is doing to that "location". 我可以手动完成剩下的工作,但所有这一切都是将.net核心正在进行的幕后工作转移到那个“位置”。

I suppose I could just do a check every time a user hits a page on the site when they're authenticated to see if I need to add or update their local session, but that seems overly ghetto to me. 我想我每次用户点击网站上的页面进行身份验证时都可以进行检查,看看我是否需要添加或更新本地会话,但这对我来说似乎过于无聊。 I want to be able to do this once per session as soon as they log in. I'll be the first to admit that my skills with Identity are pretty weak so maybe I'm missing something there? 我希望能够在每次登录后立即执行此操作。我将是第一个承认我的身份技能非常弱的人,所以也许我在那里缺少一些东西?

any guidance or suggestions would be greatly appreciated. 任何指导或建议将不胜感激。

TIA TIA

I figured out a way to do this but I"m not sure if this is the best approach or not. I created a helper class with a static method that I invoke inside of the Options.Events.OnCreatingTicket event inside my AddAuthentication() inside startup.cs. 我找到了一种方法来做到这一点,但我不确定这是否是最好的方法。我创建了一个带有静态方法的辅助类,我在AddAuthentication()里面的Options.Events.OnCreatingTicket事件中调用startup.cs。

From here, I can loop over my claims list, save the user to my local datastore and either create or update local user information. 从这里开始,我可以遍历我的声明列表,将用户保存到我的本地数据存储区,并创建或更新本地用户信息。

If this is not the best way to handle this, I'd love some better direction. 如果这不是解决这个问题的最好办法,我会喜欢一些更好的方向。

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

and my helper class: 和我的助手班:

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.

相关问题 如何从Facebook OAuth登录名将用户的电子邮件返回到我的asp.net网络表单应用程序? - How can I get the user's email returned to my asp.net web forms app from Facebook OAuth login? 没有外部登录提供程序的 ASP.NET Core MVC 2.2 登录 - ASP.NET Core MVC 2.2 login without external login providers .NET Core 2.2 - 无法从 Azure AppConfig 获取应用程序配置 - .NET Core 2.2 - Unable to get app configuration from Azure AppConfig 使用.Net Core 2.2 Web应用程序配置Azure AD登录 - Configuring Azure AD login with .Net Core 2.2 Web Application 如何在ASP.net Core 2.2中为我的CRUD Web应用程序实现基本安全性? - How to implement basic security for my CRUD web app in ASP.net Core 2.2? 在不使用身份的情况下使用自定义数据库表在ASP.NET Core 2.2中实现社交登录 - Implementing social login in asp.net core 2.2 with custom db tables without using identity 在 .NET Core C# 控制台应用程序中使用 HttpClient 进行 Google 社交登录? - Google social login with HttpClient in .NET Core C# console app? 如何测试我的 ASP.NET Core 2.2 Web API GET IActionResult 返回 Ok(object)? - How to test my ASP.NET Core 2.2 Web API GET IActionResult that returns Ok(object)? ASP.NET CORE 2.2 WEB APP 中的 Session 过期问题 - Session expire problen in ASP.NET CORE 2.2 WEB APP ASP.Net Core 2.2 Web App中的自定义身份 - Custom Identity in ASP.Net Core 2.2 Web App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM