Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Facebook;
- using System.Web.Security;
- //LoginController
- namespace MVC_Test_2.Controllers
- {
- public class LoginController : Controller
- {
- private string fbClientId = "Client-Id";
- private string fbAppSecret = "APPSECRET-Hash";
- // GET: Login
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult index(FormCollection coll)
- {
- try
- {
- string username = coll["username"];
- string password = coll["password"];
- if (username.Equals("") || password.Equals(""))
- {
- ViewBag.msg = "Fields Can Not Be Empty";
- return View();
- }
- else if (username.Equals("admin") || password.Equals("admin"))
- {
- Session["username"] = username;
- return RedirectToAction("Index", "Home");
- //return Content(coll["username"] + " " + coll["password"]);
- }
- else {
- ViewBag.msg = "Incorrect Username or Password";
- return View();
- }
- }
- catch (Exception e)
- {
- ViewBag.msg = "Exception Caught";
- return View();
- }
- }
- private Uri RedirectUri
- {
- get {
- var uriBuilder = new UriBuilder(Request.Url);
- uriBuilder.Query = null;
- uriBuilder.Fragment = null;
- uriBuilder.Path = Url.Action("FacebookCallback");
- return uriBuilder.Uri;
- }
- }
- [AllowAnonymous]
- public ActionResult Facebook()
- {
- var fb = new FacebookClient();
- var loginUrl = fb.GetLoginUrl(
- new {
- client_id = this.fbClientId,
- client_secret = this.fbAppSecret,
- redirect_uri = RedirectUri.AbsoluteUri,
- response_type = "code",
- scope = "email"
- });
- return Redirect(loginUrl.AbsoluteUri);
- }
- public ActionResult FacebookCallback(string code)
- {
- try
- {
- var fb = new FacebookClient();
- dynamic result = fb.Post("oauth/access_token", new
- {
- client_id = this.fbClientId,
- client_secret = this.fbAppSecret,
- redirect_uri = RedirectUri.AbsoluteUri,
- code = code
- });
- var accessToken = result.access_token;
- Session["AccessToken"] = accessToken;
- fb.AccessToken = accessToken;
- dynamic me = fb.Get("me?fields=link,first_name,currency,last_name,email,gender,locale,timezone,verified,picture,age_range");
- string email = me.email;
- TempData["email"] = me.email;
- TempData["first_name"] = me.first_name;
- TempData["lastname"] = me.last_name;
- TempData["picture"] = me.picture.data.url;
- FormsAuthentication.SetAuthCookie(email, false);
- Session["facebook_login_client"] = true;
- return RedirectToAction("Index", "Home");
- }
- catch (Exception e)
- {
- return RedirectToAction("Index");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement