【问题标题】:asp.net mvc API get facebook tokenasp.net mvc API 获取 facebook 令牌
【发布时间】:2015-09-11 06:52:20
【问题描述】:

我已经使用 asp.net mvc api 与用户创建了一个应用程序,遵循this tutorial。一切都很好。本地用户和 facebook 登录工作正常。

但现在我正在尝试使用 FacebookClient 并获取用户信息和朋友。 但它要求我提供令牌,但与我的会话或 cookie 中存储的令牌不同。

在 api 控制器帐户中,在 GetExternalLogin 操作中,我有以下代码:

if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
                   OAuthDefaults.AuthenticationType);
                ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
                    CookieAuthenticationDefaults.AuthenticationType);

                AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable<Claim> claims = externalLogin.GetClaims();
                ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

在这里我可以找到声明。但不是如何使用这个

我的问题是登录后如何将 facebook 令牌存储在我的 api 上?

【问题讨论】:

  • 自从您发布这个问题很长时间以来,您是否找到了如何在数据库中保存用户/登录/声明。如果用户已经注册,如何处理。

标签: asp.net asp.net-mvc facebook


【解决方案1】:
var options = new FacebookAuthenticationOptions
{
    AppId = "Your App ID",
    AppSecret = "Your App Secret",
    Provider = new FacebookAuthenticationProvider
    {
        OnAuthenticated = async context =>
        {
            // Retrieve the OAuth access token to store for subsequent API calls
            string accessToken = context.AccessToken;

            // Retrieve the username
            string facebookUserName = context.UserName;

            // You can even retrieve the full JSON-serialized user
            var serializedUser = context.User;
        }
    }
};

app.UseFacebookAuthentication(options);

有关如何执行此操作的更多信息,请参见此处:http://www.oauthforaspnet.com/providers/facebook/ f OnAuthenticated 您还可以添加:

context.Identity.AddClaim(new Claim("FacebookToken", accessToken));

我很确定此时我们已经创建了用户并创建了他们的声明,所以你

【讨论】:

  • 是的,它有帮助,但声明未保存。当我在 context.Identity.AddClaim(new Claim("FacebookToken", accessToken));并复制 accessToken 并手动使用它我可以使用它。我如何才能获得此声明?
  • 如果您想保存访问令牌,您可以使用(我认为)Id 来搜索您保存在存储机制中的客户端。如果不存在用户,您将创建一个与 facebook 具有此关联的用户,并将代码存储在关联表中。我的意思是,您将用户保存为一条记录,并将 facebook id + 链接到另一条记录中的用户,这样您就可以为一个用户进行多次登录。
  • 如果您使用 ASP.NET 身份,您可以修改 AspNetUserLogins 表,使其也包含 ApiAccessCode,然后在进行身份验证时,我很确定 ASP.NET 身份应该已经进入 AspNetUserLogins 表,所以您只需查找条目并添加代码
  • 不应类似于 ClaimsIdentity ext = await Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);?
  • 要让您的声明查看此答案:stackoverflow.com/questions/21404935/…,您只需使用 .first where ClaimType == 您要查找的类型 (FacebookToken)
猜你喜欢
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多