コード例 #1
0
        public HttpResponseMessage Login(string userName, string pwd)
        {
            if (!UserInfoService.Validate(userName, pwd))
            {
                return(ToHttpResponse(MessageResult.Failed("login failed")));
            }
            //todo: expired time should be configurable
            var ticket = new FormsAuthenticationTicket(0, userName, DateTime.Now,
                                                       DateTime.Now.AddHours(1), true, string.Format("{0}&{1}", userName, pwd),
                                                       FormsAuthentication.FormsCookiePath);
            var oUser = new UserInfo {
                UserName = userName, Password = pwd, Ticket = FormsAuthentication.Encrypt(ticket)
            };

            var identity  = new UserIdentity(oUser);
            var principal = new System.Security.Principal.GenericPrincipal(identity, null);


            if (HttpContext.Current != null)
            {
                HttpContext.Current.User = principal;
            }

            return(ToHttpResponse(MessageResult.Ok(oUser, "login succeed")));
        }
コード例 #2
0
        protected override void HandleUnauthorizedRequest(HttpActionContext actioncontext)
        {
            base.HandleUnauthorizedRequest(actioncontext);


            //return
            var response = actioncontext.Response = actioncontext.Response ?? new HttpResponseMessage();

            response.StatusCode = HttpStatusCode.Forbidden;
            var content = MessageResult.Failed("your request authentication is failed");

            response.Content = new StringContent(Json.Encode(content), Encoding.UTF8, "application/json");
        }
コード例 #3
0
        public async Task <MessageResult> ProcessMessage(MessageViewModel model, string ipAddress)
        {
            var message = ContactFormMessage.FromViewModel(model, ipAddress);

            if (string.IsNullOrEmpty(message.Subject))
            {
                var form = await GetFormSettings().ConfigureAwait(false);

                message.Subject = form.NotificationSubject;
            }

            int successCount = 0;
            int failureCount = 0;
            var errorList    = new List <MessageError>();

            foreach (var processor in _messageProcessors)
            {
                try
                {
                    var tempResult = await processor.Process(message).ConfigureAwait(false);

                    if (tempResult.Succeeded)
                    {
                        successCount += 1;
                    }
                    else
                    {
                        failureCount += 1;
                        errorList.AddRange(tempResult.Errors);
                    }
                }
                catch (Exception ex)
                {
                    failureCount += 1;
                    _log.LogError("error processing contact form message", ex);
                    var me = new MessageError
                    {
                        Code        = "processfailure",
                        Description = ex.Message
                    };
                    errorList.Add(me);
                }
            }

            if (successCount > 0)
            {
                return(MessageResult.Success);
            }

            return(MessageResult.Failed(errorList.ToArray()));
        }
コード例 #4
0
 public HttpResponseMessage AddTask(TodoTaskReqParameter parameter)
 {
     try
     {
         var model = this.GetTaskModel(parameter);
         var res   = TaskService.AddTask(model);
         return(ToHttpResponse(MessageResult.Ok(res, "add task succeed")));
     }
     catch (Exception ex)
     {
         LoggerService.LogError(ex);
         return(ToHttpResponse(MessageResult.Failed("add task failed")));
     }
 }
コード例 #5
0
 // GET api/values
 public HttpResponseMessage GetByUser()
 {
     try
     {
         var username = this.User.Identity.Name;
         var res      = TaskService.GetTasksByUser(username);
         return(ToHttpResponse(MessageResult.Ok(res, "get my tasks succeed")));
     }
     catch (Exception ex)
     {
         LoggerService.LogError(ex);
         return(ToHttpResponse(MessageResult.Failed("get my tasks failed")));
     }
 }
コード例 #6
0
 /// <summary>
 /// complete one task
 /// </summary>
 /// <param name="taskId">table key</param>
 /// <returns></returns>
 public HttpResponseMessage CompleteTask(int taskId)
 {
     try
     {
         var res = TaskService.CloseTask(taskId);
         if (res)
         {
             return(ToHttpResponse(MessageResult.Ok(null, "task completed successfully")));
         }
         else
         {
             return(ToHttpResponse(MessageResult.Failed("complete task failed")));
         }
     }
     catch (Exception ex)
     {
         LoggerService.LogError(ex);
         return(ToHttpResponse(MessageResult.Failed("complete task failed")));
     }
 }
        public async Task <MessageResult> Process(ContactFormMessage message)
        {
            var form = await contactFormResolver.GetCurrentContactForm().ConfigureAwait(false);

            var smtpOptions = await smtpOptionsProvider.GetSmtpOptions().ConfigureAwait(false);

            var errorList = new List <MessageError>();

            if (string.IsNullOrEmpty(smtpOptions.Server))
            {
                throw new InvalidOperationException("smtp settings are not configured");
            }

            EmailSender sender = new EmailSender();

            try
            {
                var plainTextMessage
                    = await viewRenderer.RenderViewAsString <ContactFormMessage>(messageProcessorOptions.NotificationTextViewName, message);

                var htmlMessage
                    = await viewRenderer.RenderViewAsString <ContactFormMessage>(messageProcessorOptions.NotificationHtmlViewName, message);

                var replyTo = message.Email;
                await sender.SendMultipleEmailAsync(
                    smtpOptions,
                    form.NotificationEmailCsv,
                    smtpOptions.DefaultEmailFromAddress,
                    message.Subject,
                    plainTextMessage,
                    htmlMessage,
                    replyTo
                    ).ConfigureAwait(false);

                if (form.CopySubmitterEmailOnSubmission)
                {
                    try
                    {
                        plainTextMessage
                            = await viewRenderer.RenderViewAsString <ContactFormMessage>(
                                  messageProcessorOptions.SubmitterNotificationTextViewName,
                                  message);

                        htmlMessage
                            = await viewRenderer.RenderViewAsString <ContactFormMessage>(
                                  messageProcessorOptions.SubmitterNotificationHtmlViewName,
                                  message);

                        await sender.SendEmailAsync(
                            smtpOptions,
                            message.Email,
                            smtpOptions.DefaultEmailFromAddress,
                            message.Subject,
                            plainTextMessage,
                            htmlMessage
                            ).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        log.LogError("error sending contact form submitter notification email", ex);
                        var m = new MessageError();
                        m.Code        = "SubmitterNotificationError";
                        m.Description = ex.Message;
                        errorList.Add(m);
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError("error sending contact form notification email: " + ex.Message, ex);
                var m = new MessageError();
                m.Code        = "NotificationError";
                m.Description = ex.Message;
                errorList.Add(m);
            }

            if (errorList.Count > 0)
            {
                return(MessageResult.Failed(errorList.ToArray()));
            }

            return(MessageResult.Success);
        }
コード例 #8
0
        public async Task <MessageResult> Process(ContactFormMessage message)
        {
            var errorList = new List <MessageError>();

            var sender = await _emailSenderResolver.GetEmailSender();

            if (sender == null)
            {
                var logMessage = $"failed to send account confirmation email because email settings are not populated";
                _log.LogError(logMessage);
                var m = new MessageError
                {
                    Code        = "NoSenderError",
                    Description = logMessage
                };
                errorList.Add(m);

                return(MessageResult.Failed(errorList.ToArray()));
            }

            var form = await _contactFormResolver.GetCurrentContactForm().ConfigureAwait(false);

            message.Tenant = await _tenantResolver.ResolveTenant();

            try
            {
                var plainTextMessage
                    = await _viewRenderer.RenderViewAsString <ContactFormMessage>(_messageProcessorOptions.NotificationTextViewName, message);

                var htmlMessage
                    = await _viewRenderer.RenderViewAsString <ContactFormMessage>(_messageProcessorOptions.NotificationHtmlViewName, message);

                var replyTo = message.Email;
                await sender.SendEmailAsync(
                    form.NotificationEmailCsv,
                    null,
                    message.Subject,
                    plainTextMessage,
                    htmlMessage,
                    replyTo
                    ).ConfigureAwait(false);

                if (form.CopySubmitterEmailOnSubmission)
                {
                    try
                    {
                        plainTextMessage
                            = await _viewRenderer.RenderViewAsString <ContactFormMessage>(
                                  _messageProcessorOptions.SubmitterNotificationTextViewName,
                                  message);

                        htmlMessage
                            = await _viewRenderer.RenderViewAsString <ContactFormMessage>(
                                  _messageProcessorOptions.SubmitterNotificationHtmlViewName,
                                  message);

                        await sender.SendEmailAsync(
                            message.Email,
                            null,
                            message.Subject,
                            plainTextMessage,
                            htmlMessage
                            ).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        _log.LogError($"error sending contact form submitter notification email {ex.Message} : {ex.StackTrace}");
                        var m = new MessageError
                        {
                            Code        = "SubmitterNotificationError",
                            Description = ex.Message
                        };
                        errorList.Add(m);
                    }
                }
            }
            catch (Exception ex)
            {
                _log.LogError($"error sending contact form notification email: {ex.Message} : {ex.StackTrace}");
                var m = new MessageError
                {
                    Code        = "NotificationError",
                    Description = ex.Message
                };
                errorList.Add(m);
            }

            if (errorList.Count > 0)
            {
                return(MessageResult.Failed(errorList.ToArray()));
            }

            return(MessageResult.Success);
        }