예제 #1
0
 public Model.Korisnici LoginMobile(KorisniciLoginRequest request)
 {
     return(_service.LoginMobile(request));
 }
예제 #2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }


            //Model.Korisnici user = null;
            var context = "";

            try
            {
                context = Request.Headers["Context"];
                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];


                if ("Registracija".Equals(context))
                {
                    RegistrovaniKorisnik = _userService.LoginMobile(new Model.Requests.KorisniciLoginRequest()//dodaj await
                    {
                        Username = username,
                        Password = password
                    });
                }
                else
                {
                    PrijavljeniKorisnik = _userService.Login( //dodaj await
                        new Model.Requests.KorisniciLoginRequest()
                    {
                        Username = username,
                        Password = password
                    });     //Authenticiraj(username, password);
                }
            }
            catch
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }
            var claims = new List <Claim>();

            if ("Registracija".Equals(context))
            {
                if (RegistrovaniKorisnik == null)
                {
                    return(AuthenticateResult.Fail("Invalid Username or Password"));
                }
            }
            else
            {
                if (PrijavljeniKorisnik == null)
                {
                    return(AuthenticateResult.Fail("Invalid Username or Password"));
                }
            }

            claims = new List <Claim> {
                new Claim(ClaimTypes.NameIdentifier, PrijavljeniKorisnik.KorisnickoIme),
                new Claim(ClaimTypes.Name, PrijavljeniKorisnik.Ime),
            };

            claims.Add(new Claim(ClaimTypes.Role, PrijavljeniKorisnik.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));
        }