[英]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 . 基本思想是您可以使用AddOAuth或AddOpenIdConnect中间件来触发社交登录。
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.