简体   繁体   English

在不使用身份的情况下使用自定义数据库表在ASP.NET Core 2.2中实现社交登录

[英]Implementing social login in asp.net core 2.2 with custom db tables without using identity

I am trying to implement social login features in asp.net core 2.2 without using default feature as given here . 我试图在asp.net core 2.2中实现社交登录功能,而不使用此处给出的默认功能。 I couldn't find any where that shows implementation without using default identity. 如果不使用默认身份,我找不到任何显示实现的地方。

I have already implemented custom login mechanism to handle user authentication, ie there is a user table that stores emailid and its password. 我已经实现了自定义登录机制来处理用户身份验证,即有一个用户表存储了emailid及其密码。 When user login it will validate from user table entry. 用户登录时,它将通过用户表条目进行验证。 In the same way I want to implement social logins like facebook, twitter, linkedin, microsoft, github etc. 以同样的方式,我想实现社交登录,例如facebook,twitter,linkedin,microsoft,github等。

Once user signin using any of these social options there email will be stored in user table with their valid token. 一旦用户使用这些社交选项中的任何一个登录,电子邮件就会连同其有效令牌一起存储在用户表中。

I am able to triggered social login using this article but not able to redirect back to correct action method. 我能够使用本文触发社交登录,但无法重定向回正确的操作方法。 Its redirecting back to same action method "IActionResult Google" from where its originated. 它从其起源重定向到相同的操作方法“ IActionResult Google”。 I couldn't understand "ExternalLoginCallback". 我无法理解“ ExternalLoginCallback”。

I need to get the response returned by the social login and to retrieve user details. 我需要获取社交登录返回的响应并检索用户详细信息。

    public IActionResult Google(string provider)
    {
        provider = "Google";
        //Issue a challenge to external login middleware to trigger sign in process
        return new ChallengeResult(provider);
    }

    [AllowAnonymous]
    [HttpGet(nameof(ExternalLoginCallback))]
    public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
    {
        //Here we can retrieve the claims
        var result = await HttpContext.AuthenticateAsync("CookieAuthenticationDefaults.AuthenticationScheme");

        return null;
    }

The basic idea is your can use AddOAuth or AddOpenIdConnect middleware to trigger the social logins . 基本思想是您可以使用AddOAuthAddOpenIdConnect中间件来触发社交登录。

For example , if using OAuth 2 flow , you can use AddOAuth extension to make consume the identity provider like facebook , microsoft .... you can acquire access token which could get the current user's basic profile information , in OnCreatingTicket callback function , you can query your local database , link/create the external user in your database , and finally create authentication ticket for user sign-in . 例如,如果使用OAuth 2流,则可以使用AddOAuth扩展来使用诸如Facebook,Microsoft ...等身份提供者。您可以在OnCreatingTicket回调函数中获取访问令牌,该令牌可以获取当前用户的基本个人资料信息,您可以查询您的本地数据库,链接/创建数据库中的外部用户,最后创建用于用户登录的身份验证票证。 For example : 例如 :

https://www.jerriepelser.com/blog/authenticate-oauth-aspnet-core-2/ https://www.jerriepelser.com/blog/authenticate-oauth-aspnet-core-2/

That is similar if using OpenID Connect , the difference is you can directly get user's information in ID token : 这与使用OpenID Connect相似,不同之处在于您可以直接通过ID令牌获取用户信息:

services.AddAuthentication()
    .AddCookie()
    .AddOpenIdConnect();

You can click here and here code samples . 您可以单击此处此处的代码示例。

You can config multi authentication schema for different providers based on your requirements. 您可以根据需要为不同的提供程序配置多重身份验证架构。

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

相关问题 ASP.Net Core 2.2 Web App中的自定义身份 - Custom Identity in ASP.Net Core 2.2 Web App Asp.Net Core 2.2身份验证 - Asp.Net Core 2.2 Identity Authentication 在创建新 cookie 之前检查声明(电子邮件),没有 asp。 使用asp net core社交登录的核心身份存储 - Check claims (email) before creating a new cookie, without asp. core identity store using asp net core social login Asp.Net Core Identity 2.2和Identity Server 4,更改用户ID类型会导致数据库上下文错误 - Asp.Net Core Identity 2.2 and Identity Server 4, Changing User Id type results in an error in the db context 具有社交登录且没有MVC依赖关系的ASP.NET WebAPI 2.2 SPA - ASP.NET WebAPI 2.2 SPA with social login and no MVC dependencies ASP.NET (6.0) Core MVC Login and Registration using Identity - ASP.NET (6.0) Core MVC Login and Registration using Identity 通过ASP.Identity表自动访问ASP.NET WEPAPI 2.2 - Autofac into ASP.NET WEPAPI 2.2 with Asp.Identity Tables 在asp.net核心标识中使用选择性表 - using selective tables in asp.net core identity 没有外部登录提供程序的 ASP.NET Core MVC 2.2 登录 - ASP.NET Core MVC 2.2 login without external login providers 使用OwinContext的ASP.net身份自定义表 - ASP.net Identity Custom Tables using OwinContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM