首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OpenIdConnectServer并尝试通过API服务连接Facebook

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

Stack Overflow用户
提问于 2016-08-18 01:33:29
回答 1查看 1.8K关注 0票数 7

我正在设法使我的API能够将Facebook上的用户与我的身份用户联系起来。

应用程序上下文

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

现在,我正试图与Facebook进行整合,但没有找到实现这一目标的方法。

我想在API中创建一个服务,比如/api/auth/login-facebook/,从Facebook传递access-token,但是我需要将access-token应用程序的access-token返回给移动应用程序,这样移动应用程序就可以调用所有其他需要授权的服务。

对此有什么帮助吗?

一种我想要得到的东西的视觉方式:

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

如果我的知识是错误的,我应该以另一种方式进行集成/登录,请告诉我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-18 12:18:51

您所描述的流非常类似于“断言授权”,这是去年的标准化流。

要使用这个流程,通常必须从外部提供者(例如JWT或SAML断言)检索标准令牌,这样您自己的授权服务器就可以验证它并提取它公开的声明。不幸的是,这是你不能做的事情,与Facebook或大多数社会服务提供商在一般情况下。

新的OAuth2草案将来可能会帮助改变这一点,但主要的服务可能需要一段时间才能开始实现它。

好消息是,在此期间,没有什么可以阻止您创建自己的"Facebook访问令牌“授予类型。下面是如何使用ASOS beta6实现断言授予:

代码语言:javascript
复制
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);
}

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39008642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档