Getting Basic Info from Google and Facebook through OWIN GetExternalLoginInfoAsync()

淺唱寂寞╮ 提交于 2019-12-07 23:20:44

问题


I am another MVC dummy and trying to work out external logins for my upcoming projects. I am using the default MVC template for that comes with VS2013. After following the exact steps as told in OWIN tutorial . The problem I have is that when a user is logging in from Google or Facebook or any other external account, he expects not to put his information again. The code I found for enabling logging in through Facebook and Google, it just fetches the username which is useless. I mean i know it would work still then but what if I want to get the basic information which facebook had already told the user I would be accessing. This is what I get in reply after successful login. Is there a way to get the basic information using this.

ADDITION: I also included Facebook package from Nuget and was able to get some info through the following code. NOW THE PROBLEM comes down to, how to I ask the user for more than just normal info eg posting on his behalf, getting posts etc using this OWIN's library.

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo.Login.LoginProvider.ToLower() == "facebook")
        {
            var accessToken = "something";
            var client = new FacebookClient(accessToken);
            dynamic me = client.Get(loginInfo.DefaultUserName);
        }


回答1:


You can do it by adding Scopes when requesting the initial login. There is an excellent sample is explained here

Bit of code:

List<string> scope = newList<string>() { "email", "user_about_me", "user_hometown", "friends_about_me", "friends_photos" };
 var x = newFacebookAuthenticationOptions();
 x.Scope.Add("email");
 x.Scope.Add("friends_about_me");
 x.Scope.Add("friends_photos");
 x.AppId = "636919159681109";
 x.AppSecret = "f3c16511fe95e854cf5885c10f83f26f";
 x.Provider = newFacebookAuthenticationProvider()
{
    OnAuthenticated = async context =>
    {
         //Get the access token from FB and store it in the database and
        //use FacebookC# SDK to get more information about the user
        context.Identity.AddClaim(
        new System.Security.Claims.Claim("FacebookAccessToken",
                                             context.AccessToken));
    }
};
 x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
 app.UseFacebookAuthentication(x);


来源:https://stackoverflow.com/questions/19989701/getting-basic-info-from-google-and-facebook-through-owin-getexternallogininfoasy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!