Ejemplo n.º 1
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                logger.LogInformation($"{context.Request.Path}\tRECEIVED");
                var sw = new Stopwatch();
                sw.Start();

                //Log activty through Sentry Breadcrumb
                var crumb = new Breadcrumb("LoggerMiddleware");
                crumb.Message = $"{context.Request.Method} {context.Request.Path}{context.Request.QueryString.ToUriComponent()}";
                crumb.Data    = new Dictionary <string, string>()
                {
                    { "IsAuthenticated", context.User.Identity.IsAuthenticated.ToString() },
                    { "Authentication", context.User.Identity.IsAuthenticated ? context.User.Identity.Name : "Unknown" }
                };
                raven.AddTrail(crumb);

                //Log activty through API
                var activity = new LoggingActivityModel()
                {
                    IP      = context.Connection.RemoteIpAddress == null ? "N/A" : context.Connection.RemoteIpAddress.ToString(),
                    User    = (context.User.Identity.IsAuthenticated == false) ? "Guest" : context.User.FindFirst(ClaimTypes.Name).Value,
                    Request = context.Request.Path
                };
                AppTask.Forget(() => appLogging.LogActivity(activity));

                //Start processing the request
                try
                {
                    await _next.Invoke(context);
                }
                catch (Exception e)
                {
                    //Allow Postgres to receive querystring
                    var feature = new LoggerFeature()
                    {
                        Path = context.Request.Path + context.Request.QueryString.ToString()
                    };
                    context.Features.Set <LoggerFeature>(feature);

                    //Try to send the request to Sentry
                    await raven.CaptureNetCoreEventAsync(e);

                    //rethrow so we can redirect to Error page
                    throw e;
                }

                //Stop request timing and output time
                sw.Stop();
                logger.LogInformation($"{context.Request.Path}\t{sw.Elapsed.TotalMilliseconds}(ms)");
            }
            catch (Exception e)
            {
                //Try to send the request to Sentry
                await raven.CaptureNetCoreEventAsync(e);
            }
        }
Ejemplo n.º 2
0
        public async Task <BaseServiceResponse> Register(UserNewModel model)
        {
            var response = new BaseServiceResponse();

            if (model.Username == null || model.Password == null || model.Email == null)
            {
                return(response);
            }
            model.Username = model.Username.ToLower();
            model.Email    = model.Email.ToLower();

            //Convert to new User Model
            var userModel = new UserModel()
            {
                Username = model.Username.ToLower(),
                Password = model.Password,
                Email    = model.Email.ToLower(),
                Meta     = new UserMetaModel()
                {
                    PrimaryUse = model.PrimaryUse,
                    Referral   = model.Referral
                }
            };

            var hash = hasher.Hash(userModel.Password);

            userModel.Password = hash.Hash;
            userModel.Salt     = hash.Salt;

            var exists = await db.Exists(userModel);

            if (exists.Email == model.Email)
            {
                return new BaseServiceResponse()
                       {
                           Code = 409, Message = "Email already in use"
                       }
            }
            ;
            else if (exists.Username == model.Username)
            {
                return new BaseServiceResponse()
                       {
                           Code = 409, Message = "Username already in use"
                       }
            }
            ;

            try
            {
                await db.InsertAsync(userModel);
            }
            catch (PostgresDuplicateValueException e)
            {
                var sentry = new SentryEvent(e);
                await raven.CaptureNetCoreEventAsync(sentry);

                //Should not get this exception
                throw e;
            }
            catch (Exception e)
            {
                var sentry = new SentryEvent(e);
                await raven.CaptureNetCoreEventAsync(sentry);
            }

            AppTask.Forget(() => email.SendWelcomeTo(new WelcomeModel()
            {
                Address = userModel.Email,
                Subject = "Welcome to Aliseeks",
                User    = userModel.Username
            }));

            return(response);
        }