Monday, 30 November 2015

ASP.Net Identity 2 Authentication Via Facebook Setup - Step 28

> C#/.Net Samples
> .Net Authentication
> Setting up ASP.NET Identity 2 with External Logins Facebok Google Microsoft and Twitter

Step # 28

 

For other steps follow:

Setting up ASP.NET Identity 2 with External Logins Facebook Google Microsoft and Twitter


Go to https://developers.facebook.com/apps to setup id and secret
 
·         Create a new app
·         Then Enter
o    an appropriate app name
o    a category: Apps for pages
o    click create app id
·         Got to settings and pick up appid and secret

By specifying the scope alone in StartUp.Auth.cs will not give the user personal information. We’ll need to use the graph api to get the data

Check this for more information

and


and then add the following nugget package


https://www.nuget.org/packages/Facebook/7.0.6









Add the following to the StartUp.Auth.cs for facebook
 
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
 
var fbOptions = new FacebookAuthenticationOptions();
            fbOptions.Scope.Add("email");
            fbOptions.Scope.Add("public_profile");
            fbOptions.Scope.Add("user_birthday");
            fbOptions.Scope.Add("user_location");
            fbOptions.AppId = "1070612249638641";
            fbOptions.AppSecret = "5cc666e9ba9a222a3205d6585d2f867e";
            fbOptions.Provider = new FacebookAuthenticationProvider()
            {
                OnAuthenticated = async context =>
                {
                    context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                    foreach (var claim in context.User)
                    {
                        var claimType = string.Format("urn:facebook:{0}", claim.Key);
                        var claimValue = claim.Value.ToString();
                        if (!context.Identity.HasClaim(claimType, claimValue))
                            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
 
                    }
 
                }
            };
            fbOptions.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
            app.UseFacebookAuthentication(fbOptions);





Social Media Links

External Login Basics
Google Setup
Facebook Setup
Twitter Setup
Microsoft Setup
External Login Controller Actions