asp.net mvc的API获取Facebook的令牌 [英] asp.net mvc API get facebook token

查看:158
本文介绍了asp.net mvc的API获取Facebook的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了asp.net mvc的API与用户,下面的本教程。一切伟大工程这样。本地用户和Facebook登录正常工作。

但现在我试图使用FacebookClient并获得用户信息和朋友。
但它要求我的道理,但是,这不是存储在我的会话cookie的还是同样的道理。

在API控制器帐户,在GetExternalLogin行动我有这个code:

 如果(hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);                ClaimsIdentity oAuthIdentity =等待user.GenerateUserIdentityAsync(的UserManager,
                   OAuthDefaults.AuthenticationType);
                ClaimsIdentity cookieIdentity =等待user.GenerateUserIdentityAsync(的UserManager,
                    CookieAuthenticationDefaults.AuthenticationType);                AuthenticationProperties性能= ApplicationOAuthProvider.CreateProperties(user.UserName);
                Authentication.SignIn(属性,oAuthIdentity,cookieIdentity);
            }
            其他
            {
                IEnumerable的<权利要求GT;索赔= externalLogin.GetClaims();
                ClaimsIdentity身份=新ClaimsIdentity(索赔,OAuthDefaults.AuthenticationType);
                Authentication.SignIn(身份);
            }

在这里我能找到的索赔。但不知道如何使用这个

我的问题是我怎么能存储在我的API Facebook的令牌登录后?


解决方案

  VAR选项=新FacebookAuthenticationOptions
{
    的AppId =您的应用程序ID,
    AppSecret =您的应用程序的秘密,
    供应商=新FacebookAuthenticationProvider
    {
        OnAuthenticated =异步上下文=>
        {
            //检索OAuth访问令牌来存储后续API调用
            字符串的accessToken = context.AccessToken;            //检索用户名
            字符串facebookUserName = context.UserName;            //你甚至可以检索完整的JSON序列化的用户
            VAR serializedUser = context.User;
        }
    }
};app.UseFacebookAuthentication(选件);

有关如何做到这一点的东西更多信息请看这里: http://www.oauthforaspnet.com /供应商/ Facebook的/
F中的OnAuthenticated你还可以添加:

  context.Identity.AddClaim(新索赔(FacebookToken的accessToken));

由于我pretty肯定,在这一点上,我们已经创建了用户,创造了他们的要求,所以你

I've created an application with asp.net mvc api with users, following this tutorial. And everything works great like that. local users and facebook login works fine.

But now I'm trying to use the FacebookClient and get the user info and friends. But it asks me for the token, but is not the same token that is stored on my session or cookie.

In api controller Account, in GetExternalLogin action I have this code:

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);
            }

Here I can find the claims. But not how to use this

My question is how can I store the facebook token on my api after the login?

解决方案

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);

For more info on how to do this stuff see here: http://www.oauthforaspnet.com/providers/facebook/ f the OnAuthenticated you could also add:

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

As I am pretty sure that at this point we have created the user and created their claims so you

这篇关于asp.net mvc的API获取Facebook的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆