Exemple #1
0
        private async void BtnPrijava_Click(object sender, EventArgs e)
        {
            ApiService.Username = txtKorisnickoIme.Text;
            ApiService.Password = txtLozinka.Text;
            try
            {
                var obj = await _service.Get <dynamic>(null);

                if (obj != null)
                {
                    List <Biblioteka_Model.Korisnici> entity = await _korisniciService.Get <List <Biblioteka_Model.Korisnici> >(null);

                    int id = 0;
                    foreach (var item in entity)
                    {
                        if (item.KorisnickoIme == txtKorisnickoIme.Text.Trim())
                        {
                            id = item.KorisnikId;
                        }
                    }
                    if (id != 0)
                    {
                        Biblioteka_Model.Korisnici korisnik = await _korisniciService.GetById <Biblioteka_Model.Korisnici>(id);

                        Global.PrijavljeniKorisnik = korisnik;
                        Global.klijentPrijavljen   = true;
                        MainForm frm = new MainForm();
                        frm.Show();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
 public void Bind()
 {
     Biblioteka_Model.Korisnici korisnik = Global.PrijavljeniKorisnik;
     Ime      = korisnik.Ime;
     Prezime  = korisnik.Prezime;
     Username = korisnik.KorisnickoIme;
     Email    = korisnik.Email;
     Adresa   = korisnik.Adresa;
     Telefon  = korisnik.Telefon;
 }
        async Task Login()
        {
            IsBusy = true;
            if (String.IsNullOrEmpty(Username))
            {
                await App.Current.MainPage.DisplayAlert("Greška", "Niste unijeli korisničko ime", "OK");
            }
            else if (String.IsNullOrEmpty(Password))
            {
                await App.Current.MainPage.DisplayAlert("Greška", "Niste unijeli lozinku", "OK");
            }
            else
            {
                ApiService.Username = Username;
                ApiService.Password = Password;


                try
                {
                    var obj = await _autoriService.Get <dynamic>(null);

                    if (obj != null)
                    {
                        List <Biblioteka_Model.Korisnici> entity = await _service.Get <List <Biblioteka_Model.Korisnici> >(null);

                        int id = 0;
                        foreach (var item in entity)
                        {
                            if (item.KorisnickoIme == Username)
                            {
                                id = item.KorisnikId;
                            }
                        }
                        if (id != 0)
                        {
                            Biblioteka_Model.Korisnici korisnik = await _service.GetById <Biblioteka_Model.Korisnici>(id);

                            Global.PrijavljeniKorisnik   = korisnik;
                            Global.klijentPrijavljen     = true;
                            Application.Current.MainPage = new MainPage();
                        }
                    }
                }
                catch (Exception ex)
                {
                    await App.Current.MainPage.DisplayAlert("Greška", "Prijava nije uspjela", "OK");
                }
            }
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            Biblioteka_Model.Korisnici user = null;
            try
            {
                var authHeader      = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
                var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
                var credentials     = Encoding.UTF8.GetString(credentialBytes).Split(':');
                var username        = credentials[0];
                var password        = credentials[1];
                user = _userService.Autentificiraj(username, password);
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            if (user == null)
            {
                return(AuthenticateResult.Fail("Invalid Username or Password"));
            }

            var claims = new List <Claim> {
                new Claim(ClaimTypes.NameIdentifier, user.KorisnickoIme),
                new Claim(ClaimTypes.Name, user.Ime),
                new Claim(ClaimTypes.Role, user.Uloga.Naziv)
            };

            //foreach (var role in user.KorisniciUloge)
            //{
            //    claims.Add(new Claim(ClaimTypes.Role, role.Uloga.Naziv));
            //}

            var identity  = new ClaimsIdentity(claims, Scheme.Name);
            var principal = new ClaimsPrincipal(identity);
            var ticket    = new AuthenticationTicket(principal, Scheme.Name);

            return(AuthenticateResult.Success(ticket));
        }