Advertisement
dipanjalmaitra

MVC_LoginController

Oct 10th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Facebook;
  7. using System.Web.Security;
  8. //LoginController
  9. namespace MVC_Test_2.Controllers
  10. {
  11.     public class LoginController : Controller
  12.     {
  13.         private string fbClientId = "Client-Id";
  14.         private string fbAppSecret = "APPSECRET-Hash";
  15.         // GET: Login
  16.         public ActionResult Index()
  17.         {
  18.             return View();
  19.         }
  20.         [HttpPost]
  21.         public ActionResult index(FormCollection coll)
  22.         {
  23.             try
  24.             {
  25.                 string username = coll["username"];
  26.                 string password = coll["password"];
  27.                 if (username.Equals("") || password.Equals(""))
  28.                 {
  29.                     ViewBag.msg = "Fields Can Not Be Empty";
  30.                     return View();
  31.                 }
  32.                 else if (username.Equals("admin") || password.Equals("admin"))
  33.                 {
  34.                     Session["username"] = username;
  35.                     return RedirectToAction("Index", "Home");
  36.                     //return Content(coll["username"] + " " + coll["password"]);
  37.                 }
  38.                 else {
  39.                     ViewBag.msg = "Incorrect Username or Password";
  40.                     return View();
  41.                 }
  42.             }
  43.             catch (Exception e)
  44.             {
  45.                 ViewBag.msg = "Exception Caught";
  46.                 return View();
  47.             }
  48.            
  49.         }
  50.  
  51.  
  52.         private Uri RedirectUri
  53.         {
  54.             get {
  55.                 var uriBuilder = new UriBuilder(Request.Url);
  56.                 uriBuilder.Query = null;
  57.                 uriBuilder.Fragment = null;
  58.                 uriBuilder.Path = Url.Action("FacebookCallback");
  59.                 return uriBuilder.Uri;
  60.             }
  61.         }
  62.  
  63.         [AllowAnonymous]
  64.         public ActionResult Facebook()
  65.         {
  66.             var fb = new FacebookClient();
  67.             var loginUrl = fb.GetLoginUrl(
  68.                 new {
  69.                     client_id = this.fbClientId,
  70.                     client_secret = this.fbAppSecret,
  71.                     redirect_uri = RedirectUri.AbsoluteUri,
  72.                     response_type = "code",
  73.                     scope = "email"
  74.                 });
  75.             return Redirect(loginUrl.AbsoluteUri);
  76.         }
  77.  
  78.         public ActionResult FacebookCallback(string code)
  79.         {
  80.             try
  81.             {
  82.                 var fb = new FacebookClient();
  83.  
  84.                 dynamic result = fb.Post("oauth/access_token", new
  85.                 {
  86.                     client_id = this.fbClientId,
  87.                     client_secret = this.fbAppSecret,
  88.                     redirect_uri = RedirectUri.AbsoluteUri,
  89.                     code = code
  90.                 });
  91.  
  92.                 var accessToken = result.access_token;
  93.                 Session["AccessToken"] = accessToken;
  94.                 fb.AccessToken = accessToken;
  95.                 dynamic me = fb.Get("me?fields=link,first_name,currency,last_name,email,gender,locale,timezone,verified,picture,age_range");
  96.  
  97.                 string email = me.email;
  98.  
  99.                 TempData["email"] = me.email;
  100.                 TempData["first_name"] = me.first_name;
  101.                 TempData["lastname"] = me.last_name;
  102.                 TempData["picture"] = me.picture.data.url;
  103.  
  104.                 FormsAuthentication.SetAuthCookie(email, false);
  105.                 Session["facebook_login_client"] = true;
  106.                 return RedirectToAction("Index", "Home");
  107.             }
  108.             catch (Exception e)
  109.             {
  110.                 return RedirectToAction("Index");
  111.             }
  112.  
  113.            
  114.  
  115.         }
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement