Ejemplo n.º 1
0
        public IActionResult Get([FromForm] vm.Authenticate model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

            var user = this.Users.Query.FirstOrDefault(x => x.UserName == model.UserName);

            if (user == null)
            {
                return(this.BadRequest("User not found"));
            }

            if (!PasswordProvider.Verify(model.Password, user.Password))
            {
                return(this.BadRequest("Incorrect user name or password"));
            }

            var claims = this.GetClaims(user);

            var token = this.Token.Generate(claims);

            return(this.Ok(new
            {
                access_token = token
            }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Login([FromForm] vm.Authenticate model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

            var user =
                this.Users
                .Query
                .WithPharmacy()
                .FirstOrDefault(x => x.UserName == model.UserName);

            if (user == null)
            {
                return(this.BadRequest("User not found"));
            }

            if (!PasswordProvider.Verify(model.Password, user.Password))
            {
                return(this.BadRequest("Incorrect user name or password"));
            }

            var claims = this.GetClaims(user);

            var entity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            await
            this.HttpContext
            .SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(entity));

            var referer = new Uri(this.Request.Headers["Referer"].ToString());

            var request =
                HttpUtility.ParseQueryString(referer.Query)
                .Get("ReturnUrl");

            if (user.PharmacyId.HasValue)
            {
                this.Data.Pharmacy.TryAdd(user.UserName, user.Pharmacy.Id);
            }

            return
                (!string.IsNullOrWhiteSpace(request)
                       ? this.Redirect(request)
                       : this.Redirect("/"));
        }