public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    using (var client = new HttpClient())
                    {
                        var kor = new KorisnikDTO()
                        {
                            ImeDTO          = model.Ime,
                            AdresaDTO       = model.adresa,
                            BrojTelefonaDTO = model.BrojTel,
                            PrezimeDTO      = model.Prezime,
                            SifraDTO        = model.Password,
                            UsernameDTO     = model.UserName
                        };

                        HttpResponseMessage response = client.PostAsJsonAsync <KorisnikDTO>(DefaultAPI.GetPath("Korisnik/Register").ToString(), kor).Result;

                        if (response.IsSuccessStatusCode)
                        {
                            var rez = response.Content.ReadAsAsync <bool>().Result;
                            if (rez != false)
                            {
                                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                                WebSecurity.Login(model.UserName, model.Password);
                                return(RedirectToAction("Index", "Home"));
                            }
                            response.Dispose();
                            response = client.PostAsJsonAsync <KorisnikDTO>(DefaultAPI.GetPath("Korisnik/Obrisi").ToString(), kor).Result;
                            ModelState.AddModelError("", "Greska");
                        }
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 2
0
        public JsonResult Login(string Username, string Password, bool rememberMe)
        {
            if (Username == "" || Password == "")
            {
                return(Json("Null", JsonRequestBehavior.AllowGet));
            }

            var kor = new KorisnikDTO
            {
                UsernameDTO = Username,
                SifraDTO    = Password
            };

            using (var client = new HttpClient())
            {
                HttpResponseMessage response = client.PostAsJsonAsync <KorisnikDTO>(DefaultAPI.GetPath("Korisnik/Login").ToString(), kor).Result;

                if (response.IsSuccessStatusCode)
                {
                    var rez = response.Content.ReadAsAsync <KorisnikDTO>().Result;
                    if (rez != null)
                    {
                        if (rememberMe)
                        {
                            SignIn(kor.UsernameDTO, true);
                        }
                        else
                        {
                            SignIn(kor.UsernameDTO, false);
                        }

                        return(Json("Pocetna", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        return(Json("Error", JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json("Greska", JsonRequestBehavior.AllowGet));
                }
            }
        }