Esempio n. 1
0
 public KupciVM Authenticiraj(string username, string password)
 {
     return(_service.Authenticiraj(username, password));
 }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            Model.Osoblje user            = null;
            Model.Kupci   kupac           = null;
            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];
            var           status          = context.VrstaAplikacije.First();

            //
            if (status.DesktopStatus == 1)
            {
                user = _userService.Authenticiraj(username, password);
                if (user != null)
                {
                    var claims = new List <Claim> {
                        new Claim(ClaimTypes.NameIdentifier, user.KorisnickoIme),
                        new Claim(ClaimTypes.Name, user.Ime),
                    };

                    foreach (var odjeliOsoblje in context.OdjeliOsoblje)
                    {
                        if (odjeliOsoblje.OsobljeId == user.OsobljeId)
                        {
                            claims.Add(new Claim(ClaimTypes.Role, odjeliOsoblje.OdjelId.ToString()));
                        }
                    }

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

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

                    return(AuthenticateResult.Success(ticket));
                }
            }
            else if (status.DesktopStatus == 0)
            {
                kupac = _kupacService.Authenticiraj(username, password);
                if (kupac != null)
                {
                    var claims = new List <Claim> {
                        new Claim(ClaimTypes.NameIdentifier, kupac.KorisnickoIme),
                        new Claim(ClaimTypes.Name, kupac.Ime),
                    };

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

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



                    return(AuthenticateResult.Success(ticket));
                }
            }

            //kupac = _kupacService.Authenticiraj(username, password);



            ex(null);

            return(AuthenticateResult.Fail("Invalid Auhtorization Header"));
        }