public EmptyResult LogOnPartial(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (MembershipService.ValidateUser(model.UserName, model.Password)) { if (db.Players.Where(p => p.Name == model.UserName).Count() == 0) { Player playerToAdd = db.Players.Add(new Player { Name = model.UserName }); db.SaveChanges(); FormsService.SignIn(model.UserName, false /* createPersistentCookie */); } else { Player player = db.Players.Single(p => p.Name == model.UserName); FormsService.SignIn(model.UserName, false /* createPersistentCookie */); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return new EmptyResult(); }
public ActionResult Authenticate(string returnUrl) { var response = openid.GetResponse(); if (response == null) { //Let us submit the request to OpenID provider Identifier id; if (Identifier.TryParse(Request.Form["openid_identifier"], out id)) { try { var request = openid.CreateRequest(Request.Form["openid_identifier"]); return request.RedirectingResponse.AsActionResult(); } catch (ProtocolException ex) { ViewBag.Message = ex.Message; return View("LogOn"); } } ViewBag.Message = "Invalid identifier"; return View("LogOn"); } //Let us check the response switch (response.Status) { case AuthenticationStatus.Authenticated: LogOnModel lm = new LogOnModel(); lm.OpenID = response.ClaimedIdentifier; //check if user exist MembershipUser user = MembershipService.GetUser("OPENID" + lm.OpenID); if (user != null) { lm.UserName = user.UserName; if (db.Players.Where(p => p.Name == user.UserName).Count() == 0) { Player playerToAdd = db.Players.Add(new Player { Name = user.UserName }); db.SaveChanges(); FormsService.SignIn(user.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { Player player = db.Players.Single(p => p.Name == user.UserName); FormsService.SignIn(user.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } } return View("LogOn", lm); case AuthenticationStatus.Canceled: ViewBag.Message = "Canceled at provider"; return View("LogOn"); case AuthenticationStatus.Failed: ViewBag.Message = response.Exception.Message; return View("LogOn"); } return new EmptyResult(); }
public ActionResult Handshake(string code) { //after authentication, Facebook will redirect to this controller action with a QueryString parameter called "code" (this is Facebook's Session key) //example uri: http://www.examplewebsite.com/facebook/handshake/?code=2.DQUGad7_kFVGqKTeGUqQTQ__.3600.1273809600-1756053625|dil1rmAUjgbViM_GQutw-PEgPIg. //this is your Facebook App ID string clientId = "451051788239586"; //this is your Secret Key string clientSecret = "52d898f5348ea388526e2bf98cfa14a1"; //we have to request an access token from the following Uri string url = "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}"; //your redirect uri must be EXACTLY the same Uri that caused the initial authentication handshake string redirectUri = "http://localhost:9047/Account/Handshake"; //Create a webrequest to perform the request against the Uri WebRequest request = WebRequest.Create(string.Format(url, clientId, redirectUri, clientSecret, code)); //read out the response as a utf-8 encoding and parse out the access_token WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader streamReader = new StreamReader(stream, encode); string accessToken = streamReader.ReadToEnd().Replace("access_token=", ""); streamReader.Close(); response.Close(); //set the access token to some session variable so it can be used through out the session Session["FacebookAccessToken"] = accessToken; //now that we have an access token, query the Graph Api for the JSON representation of the User url = "https://graph.facebook.com/me?access_token={0}"; //create the request to https://graph.facebook.com/me request = WebRequest.Create(string.Format(url, accessToken)); //Get the response response = request.GetResponse(); //Get the response stream stream = response.GetResponseStream(); //Take our statically typed representation of the JSON User and deserialize the response stream //using the DataContractJsonSerializer DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(FacebookUser)); FacebookUser facebookUser = new FacebookUser(); facebookUser = dataContractJsonSerializer.ReadObject(stream) as FacebookUser; //close the stream response.Close(); //capture the UserId Session["FacebookUserId"] = facebookUser.id; //Set the forms authentication auth cookie FormsAuthentication.SetAuthCookie(facebookUser.email, false); //redirect to home page so that user can start using your application LogOnModel lm = new LogOnModel(); lm.FacebookID = facebookUser.id.ToString(); //check if user exist MembershipUser user = MembershipService.GetUser("FACEBOOKID" + lm.FacebookID); //TODO if (user != null) { lm.UserName = user.UserName; if (db.Players.Where(p => p.Name == user.UserName).Count() == 0) { Player playerToAdd = db.Players.Add(new Player { Name = user.UserName }); db.SaveChanges(); FormsService.SignIn(user.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { Player player = db.Players.Single(p => p.Name == user.UserName); FormsService.SignIn(user.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } } return View("LogOn", lm); }