Poul K. Sørensen
Poul K. Sørensen

Reputation: 17540

Facebook C# authentication mixed with forms

Anyone who can provide sample, or a up to date guide on how to create a .net 4.0 MVC page that uses form authentication and facebook login at the same time.

I would like to provide the users the option to use there facebook login and if they dont have an account they can create an account on our page and use this login. (dont want to force them to create a facebook account).

I have created a MVC 3 project, added Facebook, Facebook.web, Facebook.Web.Mvc libs and do not know how to proceed. Adding login button and mixing it up with the standard forms login in asp.net.

Upvotes: 0

Views: 1184

Answers (1)

balexandre
balexandre

Reputation: 75103

If you are using forms authentication you can simple login with Facebook and use the user id upon logged in to set you the forms session.

The same way you log in a user using a username and password textbox, you simple log in the user as you already know what user_id wants to continue in your site.

when using the Facebook C# SDK, you can simple set you your APP_ID and APP_SECRET in the shared view and add a

<fb:login-button perms="email" size="medium">
        Login using your Facebook Account</fb:login-button>

Your Action will now be able o access the user basic info plus the email, use that information to store the Facebook ID as well and the next time the user connects, just use

FacebookWebContext.Current.IsAuthenticated()

and get the information from FB like

var client = new FacebookWebClient();
dynamic me = client.Get("me");

ViewBag.Name = me.name;
ViewBag.Id = me.id;
ViewBag.Email = me.email;

with all this info, just set the sessions you are using with your forms authentication provider.

Upvotes: 1

Related Questions