使用OpenIdConnectServer并尝试通过API服务连接到Facebook

Rap*_*lan 6 c# facebook openid-connect aspnet-contrib asp.net-core

我试图找到一种方法,使我的API能够将Facebook用户与我的身份用户相关联.

应用程序上下文

我正在开发一个移动应用程序(在Xamarin中),需要使用用户名/密码和Facebook进行登录.我已经设置了app.UseOpenIdConnectServer配置并创建了自定义,Provider因此我的应用程序已经在使用用户名/密码.

现在我正试图与Facebook进行这种集成,而不是找到一种方法来实现这一目标.

我正在考虑在API中创建服务,例如从Facebook /api/auth/login-facebook/传递,access-token但我需要将access-token我的API应用程序返回到移动应用程序,以便移动应用程序可以调用所有需要授权的其他服务.

对此有何帮助?

我想要获得的视觉方式:

  1. 用户在移动应用程序中按"使用Facebook登录"
  2. 移动应用程序呼叫从Facebook /api/auth/login-facebook/传递access-token
  3. 在API应用程序,我将检查access-tokenFacebook
  4. 如果用户不存在,我将使用Facebook返回的数据创建他,之后我将生成access-token授予访问我的API应用程序的权限
  5. 如果用户存在,我将生成access-token授予访问我的API应用程序的权限
  6. 返回access-token移动应用程序,以便可以调用其他服务

如果我的知识有误并且我应该以其他方式进行此集成/登录,请随意告诉我!

Kév*_*let 7

您描述的流程非常类似于"断言授予",这是去年标准化的流程.

要使用此流程,您通常必须从外部提供程序检索标准令牌(例如,JWT或SAML断言),以便您自己的授权服务器可以验证它并提取它公开的声明.不幸的是,这不是你可以用Facebook或大多数社交提供商做的事情.

新的OAuth2草案可能有助于在未来改变它,但在主要服务开始实施之前可能需要一段时间.

好消息是,在此期间没有什么能阻止您创建自己的"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);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述