Exemplo n.º 1
0
 public CommentController(
     CommentService commentService,
     ISessionBasedCaptcha captcha)
 {
     _commentService = commentService;
     _captcha        = captcha;
 }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context, ISessionBasedCaptcha captcha)
        {
            if (context.Request.Path == Options.RequestPath)
            {
                var w = Options.ImageWidth;
                var h = Options.ImageHeight;

                // prevent crazy size
                if (w > 640)
                {
                    w = 640;
                }
                if (h > 480)
                {
                    h = 480;
                }

                var bytes = captcha.GenerateCaptchaImageBytes(context.Session, w, h);

                context.Response.StatusCode  = StatusCodes.Status200OK;
                context.Response.ContentType = "image/png";
                await context.Response.Body.WriteAsync(bytes.AsMemory(0, bytes.Length), context.RequestAborted);
            }
            else
            {
                await _next(context);
            }
        }
Exemplo n.º 3
0
        public IActionResult GetCaptchaImage([FromServices] ISessionBasedCaptcha captcha)
        {
            var s = captcha.GenerateCaptchaImageFileStream(HttpContext.Session,
                                                           AppSettings.CaptchaSettings.ImageWidth,
                                                           AppSettings.CaptchaSettings.ImageHeight);

            return(s);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> NewComment(PostSlugViewModelWrapper model,
                                                     [FromServices] ISessionBasedCaptcha captcha)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new CommentResponse(false, CommentResponseCode.InvalidModel)));
                }

                if (!_blogConfig.ContentSettings.EnableComments)
                {
                    Response.StatusCode = (int)HttpStatusCode.Forbidden;
                    return(Json(new CommentResponse(false, CommentResponseCode.CommentDisabled)));
                }

                // Validate BasicCaptcha Code
                if (!captcha.ValidateCaptchaCode(model.NewCommentViewModel.CaptchaCode, HttpContext.Session))
                {
                    Logger.LogWarning("Wrong Captcha Code");
                    ModelState.AddModelError(nameof(model.NewCommentViewModel.CaptchaCode), "Wrong Captcha Code");

                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new CommentResponse(false, CommentResponseCode.WrongCaptcha)));
                }

                var commentPostModel = model.NewCommentViewModel;
                var response         = await _commentService.CreateAsync(new NewCommentRequest(commentPostModel.PostId)
                {
                    Username  = commentPostModel.Username,
                    Content   = commentPostModel.Content,
                    Email     = commentPostModel.Email,
                    IpAddress = HttpContext.Connection.RemoteIpAddress.ToString()
                });

                if (_blogConfig.NotificationSettings.SendEmailOnNewComment && null != _notificationClient)
                {
                    _ = Task.Run(async() =>
                    {
                        await _notificationClient.NotifyNewCommentAsync(response, s => Utils.MarkdownToContent(s, Utils.MarkdownConvertType.Html));
                    });
                }
                var cResponse = new CommentResponse(true,
                                                    _blogConfig.ContentSettings.RequireCommentReview ?
                                                    CommentResponseCode.Success :
                                                    CommentResponseCode.SuccessNonReview);

                return(Json(cResponse));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error NewComment");
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json(new CommentResponse(false, CommentResponseCode.UnknownError)));
            }
        }
Exemplo n.º 5
0
 public HomeController(
     IStringLocalizer <HomeController> localizer,
     GatewayDbContext dbContext,
     ISessionBasedCaptcha captcha)
 {
     _localizer = localizer;
     _dbContext = dbContext;
     _captcha   = captcha;
 }
Exemplo n.º 6
0
 public SignInModel(
     IOptions <AuthenticationSettings> authSettings,
     IMediator mediator,
     ILogger <SignInModel> logger,
     ISessionBasedCaptcha captcha)
 {
     _mediator = mediator;
     _logger   = logger;
     _captcha  = captcha;
     _authenticationSettings = authSettings.Value;
 }
Exemplo n.º 7
0
 public ResetPasswordModel(
     UserManager <ApplicationUser> userManager,
     IConfiguration config,
     IQueryHelper queryHelper,
     ISessionBasedCaptcha captcha)
 {
     _userManager = userManager;
     _config      = config;
     _queryHelper = queryHelper;
     _captcha     = captcha;
 }
Exemplo n.º 8
0
 public SignInModel(
     IOptions <AuthenticationSettings> authSettings,
     ILocalAccountService localAccountService,
     ILogger <SignInModel> logger,
     IBlogAudit blogAudit, ISessionBasedCaptcha captcha)
 {
     _localAccountService = localAccountService;
     _logger    = logger;
     _blogAudit = blogAudit;
     _captcha   = captcha;
     _authenticationSettings = authSettings.Value;
 }
Exemplo n.º 9
0
        public ImageController(
            ILogger <ImageController> logger,
            IOptions <AppSettings> settings,
            IMemoryCache memoryCache,
            IAsyncImageStorageProvider imageStorageProvider,
            ISessionBasedCaptcha captcha,
            BlogConfig blogConfig,
            BlogConfigurationService blogConfigurationService)
            : base(logger, settings, memoryCache: memoryCache)
        {
            _blogConfig = blogConfig;
            _blogConfig.Initialize(blogConfigurationService);

            _imageStorageProvider = imageStorageProvider;
            _captcha = captcha;
        }
Exemplo n.º 10
0
        public IActionResult GetCaptchaImage([FromServices] ISessionBasedCaptcha captcha)
        {
            var w = AppSettings.CaptchaSettings.ImageWidth;
            var h = AppSettings.CaptchaSettings.ImageHeight;

            // prevent crazy size
            if (w > 640)
            {
                w = 640;
            }
            if (h > 480)
            {
                h = 480;
            }

            var s = captcha.GenerateCaptchaImageFileStream(HttpContext.Session, w, h);

            return(s);
        }
Exemplo n.º 11
0
 public RegisterModel(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     ILogger <RegisterModel> logger,
     IEmailSender emailSender,
     IQueryHelper queryHelper,
     IConfiguration config,
     ISessionBasedCaptcha captcha,
     ApplicationDbContext db)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = logger;
     _emailSender   = emailSender;
     _queryHelper   = queryHelper;
     _config        = config;
     _captcha       = captcha;
     _db            = db;
 }
Exemplo n.º 12
0
 public OAuthController(
     UserManager <GatewayUser> userManager,
     SignInManager <GatewayUser> signInManager,
     ILoggerFactory loggerFactory,
     GatewayDbContext context,
     DeveloperApiService developerApiService,
     ConfirmationEmailSender emailSender,
     ISessionBasedCaptcha sessionBasedCaptcha,
     UserAppAuthManager authManager,
     AuthLogger authLogger)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _logger        = loggerFactory.CreateLogger <OAuthController>();
     _dbContext     = context;
     _apiService    = developerApiService;
     _emailSender   = emailSender;
     _captcha       = sessionBasedCaptcha;
     _authManager   = authManager;
     _authLogger    = authLogger;
 }
Exemplo n.º 13
0
        public CommentController(
            ILogger <CommentController> logger,
            IOptions <AppSettings> settings,
            IMemoryCache memoryCache,
            CommentService commentService,
            EmailService emailService,
            PostService postService,
            ISessionBasedCaptcha captcha,
            BlogConfig blogConfig,
            BlogConfigurationService blogConfigurationService,
            LinkGenerator linkGenerator)
            : base(logger, settings, memoryCache: memoryCache)
        {
            _blogConfig    = blogConfig;
            _linkGenerator = linkGenerator;
            _blogConfig.Initialize(blogConfigurationService);

            _commentService = commentService;
            _emailService   = emailService;
            _postService    = postService;
            _captcha        = captcha;
        }
Exemplo n.º 14
0
 public OAuthController(
     UserManager <GatewayUser> userManager,
     SignInManager <GatewayUser> signInManager,
     ILoggerFactory loggerFactory,
     GatewayDbContext context,
     DeveloperApiService developerApiService,
     ISessionBasedCaptcha sessionBasedCaptcha,
     UserAppAuthManager authManager,
     AuthLogger authLogger,
     IConfiguration configuration,
     CannonService cannonService)
 {
     _userManager         = userManager;
     _signInManager       = signInManager;
     _logger              = loggerFactory.CreateLogger <OAuthController>();
     _dbContext           = context;
     _apiService          = developerApiService;
     _captcha             = sessionBasedCaptcha;
     _authManager         = authManager;
     _authLogger          = authLogger;
     _cannonService       = cannonService;
     _allowRegistering    = configuration["AllowSelfRegistering"].Trim().ToLower() == true.ToString().ToLower();
     _allowPasswordSignIn = configuration["AllowPasswordSignIn"].Trim().ToLower() == true.ToString().ToLower();
 }
Exemplo n.º 15
0
        public async Task <IActionResult> NewComment(PostSlugViewModelWrapper model,
                                                     [FromServices] ISessionBasedCaptcha captcha)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Validate BasicCaptcha Code
                    if (!captcha.ValidateCaptchaCode(model.NewCommentViewModel.CaptchaCode, HttpContext.Session))
                    {
                        Logger.LogWarning("Wrong Captcha Code");
                        ModelState.AddModelError(nameof(model.NewCommentViewModel.CaptchaCode), "Wrong Captcha Code");

                        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        var cResponse = new CommentResponse(false, CommentResponseCode.WrongCaptcha);
                        return(Json(cResponse));
                    }

                    var commentPostModel = model.NewCommentViewModel;
                    var response         = await _commentService.AddCommentAsync(new NewCommentRequest(commentPostModel.PostId)
                    {
                        Username  = commentPostModel.Username,
                        Content   = commentPostModel.Content,
                        Email     = commentPostModel.Email,
                        IpAddress = HttpContext.Connection.RemoteIpAddress.ToString(),
                        UserAgent = GetUserAgent()
                    });

                    if (response.IsSuccess)
                    {
                        if (_blogConfig.EmailSettings.SendEmailOnNewComment && null != _notificationClient)
                        {
                            _ = Task.Run(async() =>
                            {
                                await _notificationClient.SendNewCommentNotificationAsync(response.Item, s => Utils.ConvertMarkdownContent(s, Utils.MarkdownConvertType.Html));
                            });
                        }
                        var cResponse = new CommentResponse(true,
                                                            _blogConfig.ContentSettings.RequireCommentReview ?
                                                            CommentResponseCode.Success :
                                                            CommentResponseCode.SuccessNonReview);

                        return(Json(cResponse));
                    }

                    CommentResponse failedResponse;
                    switch (response.ResponseCode)
                    {
                    case (int)ResponseFailureCode.EmailDomainBlocked:
                        Logger.LogWarning("User email domain is blocked.");
                        Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        failedResponse      = new CommentResponse(false, CommentResponseCode.EmailDomainBlocked);
                        break;

                    case (int)ResponseFailureCode.CommentDisabled:
                        Logger.LogWarning("Comment is disabled in settings, but user somehow called NewComment() method.");
                        Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        failedResponse      = new CommentResponse(false, CommentResponseCode.CommentDisabled);
                        break;

                    default:
                        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        failedResponse      = new CommentResponse(false, CommentResponseCode.UnknownError);
                        break;
                    }
                    return(Json(failedResponse));
                }

                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new CommentResponse(false, CommentResponseCode.InvalidModel)));
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error NewComment");
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json(new CommentResponse(false, CommentResponseCode.UnknownError)));
            }
        }
Exemplo n.º 16
0
 public HomeController(ISessionBasedCaptcha captcha)
 {
     _captcha = captcha;
 }
Exemplo n.º 17
0
 public ValidateCaptcha(ISessionBasedCaptcha captcha)
 {
     _captcha = captcha;
 }
Exemplo n.º 18
0
        public async Task <IActionResult> NewComment(Guid postId, NewCommentModel model, [FromServices] ISessionBasedCaptcha captcha)
        {
            if (!string.IsNullOrWhiteSpace(model.Email) && !Helper.IsValidEmailAddress(model.Email))
            {
                ModelState.AddModelError(nameof(model.Email), "Invalid Email address.");
                return(BadRequest(ModelState.CombineErrorMessages()));
            }

            if (!_blogConfig.ContentSettings.EnableComments)
            {
                return(Forbid());
            }

            if (!captcha.ValidateCaptchaCode(model.CaptchaCode, HttpContext.Session))
            {
                ModelState.AddModelError(nameof(model.CaptchaCode), "Wrong Captcha Code");
                return(Conflict(ModelState));
            }

            var response = await _commentService.CreateAsync(new(postId)
            {
                Username  = model.Username,
                Content   = model.Content,
                Email     = model.Email,
                IpAddress = (bool)HttpContext.Items["DNT"] ? "N/A" : HttpContext.Connection.RemoteIpAddress?.ToString()
            });
Exemplo n.º 19
0
 public ForgotPasswordModel(UserManager <ApplicationUser> userManager, IEmailSender emailSender, ISessionBasedCaptcha captcha)
 {
     _userManager = userManager;
     _emailSender = emailSender;
     _captcha     = captcha;
 }
Exemplo n.º 20
0
        public async Task <IActionResult> NewComment(Guid postId, NewCommentModel model, [FromServices] ISessionBasedCaptcha captcha)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_blogConfig.ContentSettings.EnableComments)
            {
                return(Forbid());
            }

            if (!captcha.ValidateCaptchaCode(model.CaptchaCode, HttpContext.Session))
            {
                ModelState.AddModelError(nameof(model.CaptchaCode), "Wrong Captcha Code");
                return(Conflict(ModelState));
            }

            var response = await _commentService.CreateAsync(new CommentRequest(postId)
            {
                Username  = model.Username,
                Content   = model.Content,
                Email     = model.Email,
                IpAddress = DNT ? "N/A" : HttpContext.Connection.RemoteIpAddress.ToString()
            });

            if (_blogConfig.NotificationSettings.SendEmailOnNewComment && _notificationClient is not null)
            {
                _ = Task.Run(async() =>
                {
                    await _notificationClient.NotifyCommentAsync(response,
                                                                 s => ContentProcessor.MarkdownToContent(s, ContentProcessor.MarkdownConvertType.Html));
                });
            }

            if (_blogConfig.ContentSettings.RequireCommentReview)
            {
                return(Created("moonglade://empty", response));
            }

            return(Ok());
        }