public ActionResult Register(RegisterModel model)
        {
            bool ok = true;
            if (!ModelState.IsValid)
            { ModelState.AddModelError("", "Złe dane"); ok = false; }
            {
                model.Email = model.Email.ToLower();
                model.Login = model.Login.ToLower();
                if (!ReCaptcha.Validate(privateKey: "6LcNtc8SAAAAABTcliRjCCdZyFuMyjy4TmR2S0OZ"))
                { ModelState.AddModelError("", "Błędnie przepisany kod captcha"); ok = false; }
                if (Membership.FindUsersByEmail(model.Email).Count != 0)
                { ModelState.AddModelError("", "Email w użyciu"); ok = false; }
                if (Membership.FindUsersByName(model.Login).Count != 0)
                { ModelState.AddModelError("", "Taki użytkownik już istnieje"); ok = false; }
                if (ok == true)
                {
                    // Attempt to register the user
                    MembershipCreateStatus createStatus;
                    MembershipUser user = Membership.CreateUser(model.Login, model.Password, model.Email, null, null, true, null, out createStatus);

                    if (Roles.RoleExists("User") == false)
                        Roles.CreateRole("User");
                    if (Roles.IsUserInRole(model.Login, "User") == false)
                        Roles.AddUserToRole(model.Login, "User");

                    UserData ud = new UserData();
                    ud.MembershipUserID = (Guid)user.ProviderUserKey;
                    ud.FirstName = "";
                    ud.LastName = "";
                    db.UserDatas.Add(ud);
                    db.SaveChanges();

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        FormsAuthentication.SetAuthCookie(model.Login, false /* createPersistentCookie */);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", ErrorCodeToString(createStatus));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        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 = "252734311486230";

            //this is your Secret Key
            string clientSecret = "daa2835f96c1fd0c3b04c86504096714";

            //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://*****:*****@') break;
                else Login += c;

                if (Membership.FindUsersByEmail(email).Count != 0)
                {
                    if(Membership.GetUser(email) != null)
                        Login = email;
                    FormsAuthentication.SetAuthCookie(Login, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    RegisterModel model = new RegisterModel();

                    model.Password = model.ConfirmPassword = Membership.GeneratePassword(10, 5);
                    model.Email = email;

                    model.Login = Login;

                    if (ModelState.IsValid)
                    {
                        try
                        {
                            WebMail.SmtpServer = "poczta.o2.pl";
                            WebMail.UserName = "******";
                            WebMail.Password = "******";
                            WebMail.Send(
                                    model.Email,
                                    "Hasło do serwisu Photolife",
                                    "Witaj!<br /><br />" +
                                    "Właśnie stworzyliśmy ci konto na Photolifenet.<br /><br />" +
                                    "Login: "******"<br />" +
                                    "Email: " + model.Email + "<br />" +
                                    "Hasło: " + model.Password + "<br /><br />" +
                                    "Po zalogowaniu się w systemie możesz zmienić swoje hasło.<br /><br />",
                                    "*****@*****.**"
                                );
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                        }

                        // big
                        string remoteImgPathBig = "https://graph.facebook.com/" + me.username + "/picture?type=large";
                        Uri remoteImgPathUriBig = new Uri(remoteImgPathBig);
                        string localPath = Path.Combine(Server.MapPath(Url.Content("~/Content/UserImages/")) + me.username + "big.jpg");
                        WebRequest focia = WebRequest.Create(string.Format(remoteImgPathBig, code));
                        WebResponse odpfocia = focia.GetResponse();
                        String oo = odpfocia.ResponseUri.AbsoluteUri;
                        WebClient webClient = new WebClient();
                        webClient.DownloadFile(oo, localPath);
                        // big

                        //50
                        string remoteImg50Path = "https://graph.facebook.com/" + me.username + "/picture?size=small";
                        string localPath50 = Path.Combine(Server.MapPath(Url.Content("~/Content/UserImages/")) + me.username + "50.jpg");

                        Uri remoteImg50PathUri = new Uri(remoteImg50Path);
                        WebRequest focia50 = WebRequest.Create(string.Format(remoteImg50Path, code));
                        WebResponse odpfocia50 = focia50.GetResponse();
                        String oo50 = odpfocia50.ResponseUri.AbsoluteUri;
                        WebClient webClient50 = new WebClient();
                        webClient.DownloadFile(oo50, localPath50);
                        //50

                        MembershipCreateStatus createStatus;

                        MembershipUser newuser = Membership.CreateUser(model.Login, model.Password, model.Email, null, null, true, null, out createStatus);

                        UserData ud = new UserData();
                        ud.MembershipUserID = (Guid)newuser.ProviderUserKey;
                        if((ud.FirstName = me.first_name) == null)
                            ud.FirstName = "";
                        if ((ud.LastName = me.last_name) == null)
                            ud.LastName = "";
                        db.UserDatas.Add(ud);
                        db.SaveChanges();

                        if (Roles.RoleExists("User") == false)
                            Roles.CreateRole("User");
                        if(Roles.IsUserInRole(newuser.UserName, "User") == false)
                            Roles.AddUserToRole(model.Login, "User");

                        // powiązanie fot z userem
                        //50
                        var entity50 = new PhotolifeEntities();
                        var photo50 = new Photo();
                        photo50.prefix = localPath50;
                        photo50.MembershipUserID = (Guid)newuser.ProviderUserKey;
                      //  photo50.MembershipUser = newuser;
                        entity50.Photos.Add(photo50);
                        // entity50.SaveChanges();
                        // photo50.SaveChanges();

                        //big
                        var entitybig = new PhotolifeEntities();
                        var photobig = new Photo();
                        photobig.prefix = localPath;
                        photobig.MembershipUserID = (Guid)newuser.ProviderUserKey;
                        //photobig.MembershipUser = newuser;
                        entitybig.Photos.Add(photobig);
                        // entitybig.SaveChanges();
                        // photobig.SaveChanges();

                        if (createStatus == MembershipCreateStatus.Success)
                        {
                            FormsAuthentication.SetAuthCookie(model.Login, false /* createPersistentCookie */);
                            return RedirectToAction("FacebookCreateSuccess");
                        }
                    }
                }

               // return View();

            //FormsAuthentication.SetAuthCookie(email, false);

            //return RedirectToAction("Index", "Home");

            return Content(email);
        }