Exemplo n.º 1
0
        public ActionResult JsonFacebook(FacebookProfileModel model)
        {
            if (ModelState.IsValid)
            {
                KatushaMembershipCreateStatus createStatus;
                var tmpGuid = Guid.NewGuid().ToString("N");
                var user    = UserService.CreateUser(tmpGuid, "tEmpPassword", model.Email, null, null, true, null, out createStatus);
                if (createStatus == KatushaMembershipCreateStatus.Success)
                {
                    var profile = Mapper.Map <Profile>(model);
                    profile.UserId = user.Id;
                    profile.Guid   = user.Guid;

                    ProfileService.CreateProfile(profile);
                    user.FacebookUid = model.FacebookId;
                    user.UserRole    = (long)UserRole.Normal;
                    UserService.UpdateUser(user);
                    FormsAuthentication.SetAuthCookie(tmpGuid, createPersistentCookie: false);
                    return(Json(new { success = true }));
                }
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
            var errors = GetErrorsFromModelState();

            return(Json(new { errors }));
        }
Exemplo n.º 2
0
        public ActionResult Facebook(FacebookProfileModel model)
        {
            var accessToken = (String)Session["AccessToken"];

            if (String.IsNullOrWhiteSpace(accessToken))
            {
                throw new KatushaException("FacebookAccessToken", "No Facebook Access Token");
            }
            var     client = new Facebook.FacebookClient(accessToken);
            dynamic me     = client.Get("/me?access_token=" + accessToken); //new { fields = "name,id,email,birthday" }

            //if (createStatus == KatushaMembershipCreateStatus.Success) {
            //    FormsAuthentication.SetAuthCookie(user.UserName, createPersistentCookie: false);
            //    return Json(new { status = "new", redir = Url.Action("Index", "Home"), tmp = me.id });
            //}
            return(View(me));
        }
Exemplo n.º 3
0
        public async Task SetFacebookUserProfileAsync(string accessToken)
        {
            var facebookServices = new FacebookServices();

            FacebookProfile = await facebookServices.GetFacebookProfileAsync(accessToken);
        }
Exemplo n.º 4
0
        public ActionResult Facebook()
        {
            var accessToken = (String)Session["AccessToken"];

            if (String.IsNullOrWhiteSpace(accessToken))
            {
                throw new KatushaException("FacebookAccessToken", "No Facebook Access Token");
            }
            var     client = new Facebook.FacebookClient(accessToken);
            dynamic me     = client.Get("/me?access_token=" + accessToken); //, new {fields = "name,id,email,birthday,email,gender,hometown,location,quotes,username,id"});

            var facebookLocation = "";

            if (me.location != null && !String.IsNullOrWhiteSpace(me.location.name))
            {
                facebookLocation = me.location.name;
            }
            else if (me.hometown != null && !String.IsNullOrWhiteSpace(me.hometown.name))
            {
                facebookLocation = me.hometown.name;
            }
            var location = new LocationModel {
                CityCode = 0, CountryCode = "", CityName = ""
            };

            if (!String.IsNullOrWhiteSpace(facebookLocation))
            {
                var arr = facebookLocation.Split(',');
                if (arr.Length >= 2)
                {
                    location.CountryName = arr[arr.Length - 1].Trim();
                    for (int i = 0; i < arr.Length - 1; i++)
                    {
                        location.CityName += arr[i] + ", ";
                    }
                    location.CityName = location.CityName.Substring(0, location.CityName.Length - 2);
                }
                else if (arr.Length == 1)
                {
                    location.CountryName = arr[0].Trim();
                }
            }
            var model = new FacebookProfileModel {
                Name = me.name, Description = me.quotes, Gender = (me.gender == "male") ? Sex.Male : Sex.Female, FacebookId = me.id
            };
            var countries = ResourceService.GetCountries();

            location.CountryName = location.CountryName.ToLowerInvariant();
            foreach (var item in countries.Where(item => item.Value.ToLowerInvariant() == location.CountryName.ToLowerInvariant()))
            {
                location.CountryCode = item.Key;
                location.CountryName = item.Value;
                break;
            }
            if (location.CountryCode == "")
            {
                location.CountryName = "";
            }
            if (location.CountryCode != "" && location.CityName != "")
            {
                location.CityName = location.CityName.ToLowerInvariant();
                var cities = ResourceService.GetCities(location.CountryCode);
                var found  = false;
                foreach (var item in cities.Where(item => item.Value.ToLowerInvariant() == location.CityName))
                {
                    found             = true;
                    location.CityName = item.Value;
                    location.CityCode = int.Parse(item.Key);
                    break;
                }
                if (!found)
                {
                    location.CityName = "";
                }
            }
            model.Location = location;
            return(ContextDependentView(model, "Facebook"));
        }
        public async Task <IHttpActionResult> RegisterExternalFacebook(ExternalLoginModel model)
        {
            FacebookProfileModel profile = null;

            if (model.AccessToken != null)
            {
                profile = await fbProvider.GetProfileData(model.AccessToken);
            }
            else if (model.Code != null)
            {
                profile = await model.GetProfile(this.fbProvider);
            }
            else
            {
                return(this.BadRequest(new ApiResponse(400)));
            }

            if (!await this.db.Users.AnyAsync(x => x.Email.Equals(profile.Email)))
            {
                var toCreate = new Models.User
                {
                    FirstName      = profile.First_Name,
                    LastName       = profile.Last_Name,
                    Email          = profile.Email,
                    UserName       = profile.Name,
                    EmailConfirmed = true,
                    Avatar         = profile.Picture.Data.Url,
                    Cover          = profile.Cover.Source,
                    Enabled        = true
                };

                var result = await userManager.CreateAsync(toCreate);

                if (result.Succeeded)
                {
                    result = await userManager.AddToRoleAsync(toCreate.Id, "user");
                }

                if (!result.Succeeded)
                {
                    return(this.InternalServerError(new ApiResponse(500, new { Test = "nemre u rolu dodat" })));
                }
            }

            var localUser = this.db.Users.FirstOrDefault(x => x.Email.Equals(profile.Email));

            if (localUser != null)
            {
                if (!localUser.Logins.Any(x => x.LoginProvider.Equals("facebook") && x.ProviderKey.Equals(profile.Id)))
                {
                    var addLogin = await userManager.AddLoginAsync(localUser.Id, new UserLoginInfo("facebook", profile.Id));

                    if (!addLogin.Succeeded)
                    {
                        return(this.InternalServerError(new ApiResponse(500, new { Test = "addanje logina failano" })));
                    }
                }

                var role = (await this.userManager.GetRolesAsync(localUser.Id)).First();

                ITokenBuilder builder = new TokenBuilder(localUser.Id, localUser.UserName, role);

                return(Ok(new
                {
                    Id = localUser.Id,
                    Role = role,
                    Token = builder.GetLocalAccessToken()
                }));
            }
            return(this.NotFound(new ApiResponse(404)));
        }