Beispiel #1
0
        public async Task <ActionResult> Subscribe([FromForm] SubscribeModel model)
        {
            // this.ValidateCsrfToken();
            if (!this.ModelState.IsValid)
            {
                return(BadRequest(new FormResponse {
                    Message = string.Join(", ", this.ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage)), Result = this.ModelState.AsApiResult()
                }));
            }
            try
            {
                var emailGrain = this.grainClient.GetGrain <IEmailGrain>(0);
                await emailGrain.SendEmail(new Email {
                    To = new List <string> {
                        "*****@*****.**"
                    },
                    MessageBody = $"<p>Keep me informed: {model.Email}</p>",
                    Subject     = $"Testing: subscriber request for {model.Email}",
                });

                return(Ok(new FormResponse {
                    Message = "Geregistreerd!", Result = ApiResult.AsSuccess()
                }));
            }
            catch (Exception e)
            {
                var result = new FormResponse {
                    Result = ApiResult.AsException(e, includeExceptions: true), Message = "An Error occurred :-("
                };
                return(BadRequest(result));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Login([FromBody] LoginInputModel model)
        {
            // Hack to work around rc1 bug
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            if (ModelState.IsValid)
            {
                var result = await this.signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    this.logger.LogInformation(1, "User logged in.");
                    return(Ok(new LoginResponseModel {
                        Result = ApiResult.AsSuccess()
                    }));
                }
                else
                {
                    this.logger.LogWarning(2, "User login failed.");
                    var response = new LoginResponseModel
                    {
                        IsLockedOut       = result.IsLockedOut,
                        IsNotAllowed      = result.IsNotAllowed,
                        RequiresTwoFactor = result.RequiresTwoFactor,
                        Result            = ApiResult.AsError("Login Failed")
                    };
                    return(BadRequest(response));
                }
            }
            else
            {
                return(BadRequest(ApiModel.AsError("model validation failed (TODO add the errors)")));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Login([FromBody] LoginInputModel model)
        {
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return(Ok(new LoginResultModel {
                        Result = ApiResult.AsSuccess("User logged in.")
                    }));
                    // return RedirectToLocal(model.ReturnUrl);
                }
                if (result.RequiresTwoFactor)
                {
                    return(Ok(new LoginResultModel {
                        RedirectTo = nameof(SendCode), ReturnUrl = model.ReturnUrl, RequiresTwoFactor = true, Result = ApiResult.AsSuccess("Second factor required.")
                    }));
                    // return RedirectToAction(nameof(SendCode), new { ReturnUrl = model.ReturnUrl, RememberMe = model.RememberLogin });
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    // TODO Pass ModelErrors
                    return(BadRequest(new LoginResultModel {
                        IsLockedOut = true, Result = ApiResult.AsError("User account locked out.")
                    }));
                    // return View("Lockout");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(BadRequest(new LoginResultModel {
                        Result = ApiResult.AsError("Invalid login attempt.")
                    }));
                    // return View(await _account.BuildLoginViewModelAsync(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(BadRequest(new LoginResultModel {
                Result = ApiResult.AsError("An error occurred.")
            }));
            // return View(await _account.BuildLoginViewModelAsync(model));
        }
Beispiel #4
0
 public static ApiModel AsSuccess(string message = null)
 {
     return(new ApiModel(ApiResult.AsSuccess(message)));
 }