使用 OpenIdConnectServer 并尝试通过 API 服务连接到 Facebook
Use OpenIdConnectServer and try to connect to Facebook through API service
我正在尝试找出一种方法,使我的 API 能够将 Facebook 的用户关联到我的身份用户。
应用上下文
我正在开发需要使用 Username/Password 和 Facebook 登录的移动应用程序(在 Xamarin 中)。我已经设置了 app.UseOpenIdConnectServer
配置并创建了自定义 Provider
所以我的应用程序已经可以使用 Username/Password.
现在我正在尝试与 Facebook 集成,但找不到实现此目的的方法。
我想在 API 中创建一个服务,比如 /api/auth/login-facebook/
从 Facebook 传递 access-token
但我需要 return access-token
我的 API 应用程序到移动应用程序,以便移动应用程序可以调用所有其他需要授权的服务。
有什么帮助吗?
我想要获得的视觉方式:
- 用户在移动应用程序中按 "Login with Facebook"
- 移动应用程序调用
/api/auth/login-facebook/
从 Facebook access-token
传递
- 在 API 应用程序中,我将检查
access-token
与 Facebook
包
- 如果用户不存在,我将使用 Facebook return 给我的数据创建他,然后我将生成
access-token
以授予访问我的 API 申请
- 如果用户存在,我将生成
access-token
以授予对我的 API 应用程序 的访问权限
- Return
access-token
到移动应用程序,以便它可以调用其他服务
如果我的知识有误,我应该用其他方式integration/login,请随时告诉我!
您描述的流程与去年 standardized 的“断言授权”流程非常相似。
要使用此流程,您通常必须从外部提供程序(例如 JWT 或 SAML 断言)检索标准令牌,以便您自己的授权服务器可以 validate it 并提取它公开的声明。不幸的是,这不是您可以通过 Facebook 或一般的大多数社交提供商来做的事情。
A new OAuth2 draft 将来可能会帮助改变这种情况,但可能需要一段时间才能在主要服务开始实施它。
好消息是,您可以同时创建自己的“Facebook 访问令牌”授权类型。以下是使用 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);
}
我正在尝试找出一种方法,使我的 API 能够将 Facebook 的用户关联到我的身份用户。
应用上下文
我正在开发需要使用 Username/Password 和 Facebook 登录的移动应用程序(在 Xamarin 中)。我已经设置了 app.UseOpenIdConnectServer
配置并创建了自定义 Provider
所以我的应用程序已经可以使用 Username/Password.
现在我正在尝试与 Facebook 集成,但找不到实现此目的的方法。
我想在 API 中创建一个服务,比如 /api/auth/login-facebook/
从 Facebook 传递 access-token
但我需要 return access-token
我的 API 应用程序到移动应用程序,以便移动应用程序可以调用所有其他需要授权的服务。
有什么帮助吗?
我想要获得的视觉方式:
- 用户在移动应用程序中按 "Login with Facebook"
- 移动应用程序调用
/api/auth/login-facebook/
从 Facebookaccess-token
传递 - 在 API 应用程序中,我将检查
access-token
与Facebook
包 - 如果用户不存在,我将使用 Facebook return 给我的数据创建他,然后我将生成
access-token
以授予访问我的 API 申请 - 如果用户存在,我将生成
access-token
以授予对我的 API 应用程序 的访问权限
- Return
access-token
到移动应用程序,以便它可以调用其他服务
如果我的知识有误,我应该用其他方式integration/login,请随时告诉我!
您描述的流程与去年 standardized 的“断言授权”流程非常相似。
要使用此流程,您通常必须从外部提供程序(例如 JWT 或 SAML 断言)检索标准令牌,以便您自己的授权服务器可以 validate it 并提取它公开的声明。不幸的是,这不是您可以通过 Facebook 或一般的大多数社交提供商来做的事情。
A new OAuth2 draft 将来可能会帮助改变这种情况,但可能需要一段时间才能在主要服务开始实施它。
好消息是,您可以同时创建自己的“Facebook 访问令牌”授权类型。以下是使用 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);
}