예제 #1
0
        public async Task <IActionResult> Signin([FromForm] SignInInputModel model, [FromForm] string button)
        {
            var context = await interactions.GetAuthorizationContextAsync(model.ReturnUrl);

            if ("signin" != button)
            {
                logger.LogDebug("Signin executing");

                if (null != context)
                {
                    await interactions.GrantConsentAsync(context, ConsentResponse.Denied);

                    if (await clientStore.IsPkceClientAsync(context.ClientId))
                    {
                        return(View("Redirect", new RedirectModel {
                            RedirectUrl = model.ReturnUrl
                        }));
                    }

                    return(Redirect(model.ReturnUrl));
                }

                return(Redirect("~/"));
            }

            if (ModelState.IsValid)
            {
                var result = await mediator.Send(new GetCustomerQuery(model.Email, model.Password), HttpContext.RequestAborted);

                if (false == result.IsSuccess())
                {
                    return(View());
                }

                if (result.Data.IsNotAllowed)
                {
                    return(View());
                }

                if (result.Data.IsLockedOut)
                {
                    return(View());
                }

                if (result.Data.RequiresTwoFactor)
                {
                    return(View());
                }

                if (result.Data.Success)
                {
                    var customer = result.Data.Customer;

                    await eventService.RaiseAsync(new UserLoginSuccessEvent(
                                                      IdentityServerConstants.LocalIdentityProvider,
                                                      customer.NormalizedUserName,
                                                      customer.UserName,
                                                      customer.ContactName)
                                                  );

                    await mediator.Send(new SignInCommand(customer, model.RememberMe), HttpContext.RequestAborted);

                    if (null != context)
                    {
                        if (await clientStore.IsPkceClientAsync(context.ClientId))
                        {
                            return(View("Redirect", new RedirectModel {
                                RedirectUrl = model.ReturnUrl
                            }));
                        }

                        return(Redirect(model.ReturnUrl));
                    }

                    if (Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return(Redirect(model.ReturnUrl));
                    }

                    if (String.IsNullOrEmpty(model.ReturnUrl))
                    {
                        return(Redirect("~/"));
                    }

                    var uri = new Uri(model.ReturnUrl);

                    if (uri.IsAbsoluteUri)
                    {
                        return(Redirect(model.ReturnUrl));
                    }

                    throw new Exception("Invalid redirect url");
                }
            }

            var invalidCredentials = localizer.InvalidCredentials(context?.UiLocales);
            await eventService.RaiseAsync(new UserLoginFailureEvent(model.Email, invalidCredentials));

            ModelState.AddModelError(String.Empty, "Invalid credentials");

            return(View(await CreateSigninModelAsync(model)));
        }