我正在尝试找出一种方法,使我的 API 能够将来自 Facebook 的用户关联到我的身份用户。
应用上下文
我正在开发一个移动应用程序(在 Xamarin 中),需要使用用户名/密码和 Facebook 进行登录。我已经设置了 app.UseOpenIdConnectServer
配置并创建了自定义 Provider
,所以我的应用程序已经可以使用用户名/密码了。
现在我正在尝试与 Facebook 集成,但没有找到实现此目的的方法。
我正在考虑在 API 中创建一个服务,例如 /api/auth/login-facebook/
从 Facebook 传递 access-token
但我需要返回我的 API 应用程序的 access-token
到移动应用程序,以便移动应用程序可以调用所有其他需要授权的服务。
有什么帮助吗?
我试图获得的视觉方式:
- 用户在移动应用程序中按“使用 Facebook 登录”
- 移动应用程序调用
/api/auth/login-facebook/
传递来自 Facebook 的access-token
- 在 API 应用程序中,我将使用
Facebook
包检查access-token
- 如果用户不存在,我将使用 Facebook 返回给我的数据创建他,之后我将生成
access-token
以授予对我的 API 应用程序的访问权限 - 如果用户存在,我将生成
access-token
以授予对我的 API 应用程序的访问权限 - 将
access-token
返回给移动应用程序,以便它可以调用其他服务
如果我的知识有误,我应该以其他方式进行集成/登录,请随时告诉我!
最佳答案
您描述的流程与“断言授权”非常相似,该流程是 standardized去年。
要使用此流程,您通常必须从外部提供程序(例如 JWT 或 SAML 断言)检索标准 token ,以便您自己的授权服务器可以 validate it并提取它公开的声明。不幸的是,这不是您可以通过 Facebook 或一般的大多数社交提供商来做的事情。
A new OAuth2 draft将来可能会帮助改变这种情况,但主要服务开始实现它可能需要一段时间。
好消息是,您可以同时创建自己的“Facebook 访问 token ”授权类型。以下是使用 ASOS beta6 实现断言授权的方法:
public override Task ValidateTokenRequest(ValidateTokenRequestContext context)
{
// Reject the token request if it doesn't use grant_type=password, refresh_token
// or urn:ietf:params:oauth:grant-type:facebook_access_token.
if (!context.Request.IsPasswordGrantType() &&
!context.Request.IsRefreshTokenGrantType() &&
context.Request.GrantType != "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
context.Reject(
error: OpenIdConnectConstants.Errors.UnsupportedGrantType,
description: "The specified grant type is not supported by this server.");
return Task.FromResult(0);
}
// Reject the token request if the assertion parameter is missing.
if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token" &&
string.IsNullOrEmpty(context.Request.Assertion))
{
context.Reject(
error: OpenIdConnectConstants.Errors.InvalidRequest,
description: "The assertion is missing.");
return Task.FromResult(0);
}
// Since there's only one application and since it's a public client
// (i.e a client that cannot keep its credentials private), call Skip()
// to inform the server the request should be accepted without
// enforcing client authentication.
context.Skip();
return Task.FromResult(0);
}
public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
// Only handle grant_type=password and urn:ietf:params:oauth:grant-type:facebook_access_token
// requests and let the OpenID Connect server middleware handle the refresh token requests.
if (context.Request.IsPasswordGrantType())
{
// Skipped for brevity.
}
else if (context.Request.GrantType == "urn:ietf:params:oauth:grant-type:facebook_access_token")
{
// The assertion corresponds to the Facebook access token.
var assertion = context.Request.Assertion;
// Create a new ClaimsIdentity containing the claims that
// will be used to create an id_token and/or an access token.
var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
// Validate the access token using Facebook's token validation
// endpoint and add the user claims you retrieved here.
identity.AddClaim(ClaimTypes.NameIdentifier, "FB user identifier");
// Create a new authentication ticket holding the user identity.
var ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
OpenIdConnectServerDefaults.AuthenticationScheme);
// Set the list of scopes granted to the client application.
ticket.SetScopes(new[]
{
/* openid: */ OpenIdConnectConstants.Scopes.OpenId,
/* email: */ OpenIdConnectConstants.Scopes.Email,
/* profile: */ OpenIdConnectConstants.Scopes.Profile,
/* offline_access: */ OpenIdConnectConstants.Scopes.OfflineAccess
}.Intersect(context.Request.GetScopes()));
context.Validate(ticket);
}
return Task.FromResult(0);
}
关于c# - 使用 OpenIdConnectServer 并尝试通过 API 服务连接到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39008642/