Example #1
0
        public IHttpActionResult TokenFB()
        {
            string           code       = HttpContext.Current.Request.Params["code"];
            FacebookProvider fbProvider = new FacebookProvider();

            dynamic respone = fbProvider.GetToken(code);

            string   token  = respone.access_token;
            TimeSpan expire = TimeSpan.FromSeconds(Double.Parse(respone.expires_in.ToString()));

            dynamic userDataFB = fbProvider.GetUserData(token);
            string  email      = userDataFB.email;

            List <user> users = _db.user.Where(x => x.mailFB == email).ToList();

            token tokenFB = new token()
            {
                tokenValue = token,
                expire     = DateTime.Now + expire,
                type       = 1,
                active     = true
            };

            if (users.Count == 1)
            {
                tokenFB.userId = users.First().id;

                _db.token.Add(tokenFB);
                _db.SaveChanges();

                return(Ok(token));
            }

            _cache.Add("tokenFB", respone);

            return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Account not connected")));
        }
Example #2
0
        public IHttpActionResult ConnectToFacebook(LoginModel user)
        {
            if (!_cache.Contains("tokenFB"))
            {
                return(BadRequest("First login in facebook"));
            }

            dynamic tokenFB = _cache["tokenFB"];

            _cache.Remove("tokenFB");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string hashedPassword = Auth.HashPassword(user.Password);

            List <user> users = _db.user.Where(x => x.login == user.Login && x.password == hashedPassword).ToList();

            if (users.Count != 1)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid login or password")));
            }

            FacebookProvider fbProvider = new FacebookProvider();

            string   tokenValue = tokenFB.access_token;
            TimeSpan expire     = TimeSpan.FromSeconds(Double.Parse(tokenFB.expires_in.ToString()));

            dynamic userDataFB = fbProvider.GetUserData(tokenValue);
            string  email      = userDataFB.email;

            using (DbContextTransaction dbtransaction = _db.Database.BeginTransaction())
            {
                try
                {
                    user userFromDB = users.First();
                    userFromDB.mailFB = email;

                    _db.user.Attach(userFromDB);
                    _db.Entry(userFromDB).State = EntityState.Modified;
                    _db.SaveChanges();

                    token newToken = new token()
                    {
                        tokenValue = tokenValue,
                        expire     = DateTime.Now + expire,
                        type       = 1,
                        active     = true,
                        userId     = userFromDB.id
                    };

                    _db.token.Add(newToken);
                    _db.SaveChanges();

                    dbtransaction.Commit();
                }
                catch
                {
                    dbtransaction.Rollback();
                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Connected to Facebook failed!")));
                }
            }
            return(Ok(tokenFB));
        }