Exemplo n.º 1
0
        protected PostModule(IUnitOfWork unitOfWork, ICommandHandler <TMessage> commandHandler, string path,
                             NHibernateUnitOfWork relationalUnitOfWork, EventDispatcher eventDispatcher)
        {
            this.unitOfWork           = unitOfWork;
            this.commandHandler       = commandHandler;
            this.relationalUnitOfWork = relationalUnitOfWork;
            this.eventDispatcher      = eventDispatcher;

            this.RequiresAuthentication();
            this.RequiresHttps();

            Post[path] = _ =>
            {
                CustomUserIdentity user = this.Context.CurrentUser as CustomUserIdentity;

                TMessage message = this.Bind <TMessage>();
                message.UserId    = user.Id;
                message.ProcessId = Guid.NewGuid();

                EResultCode resultCode = default(EResultCode);
                unitOfWork.DoInTransaction(() =>
                {
                    resultCode = (EResultCode)commandHandler.Handle(message);
                });

                relationalUnitOfWork.DoInTransaction(eventDispatcher.DispatchEvents);


                return(new Response()
                {
                    StatusCode = (HttpStatusCode)resultCode,
                });
            };
        }
Exemplo n.º 2
0
        protected GetModule(IUnitOfWork unitOfWork, IMessageHandler <TQuery> messageHandler, string path)
        {
            this.unitOfWork     = unitOfWork;
            this.messageHandler = messageHandler;

            this.RequiresHttps();

            Get[path] = _ =>
            {
                TQuery request = this.Bind <TQuery>();

                if (Context.CurrentUser != null)
                {
                    CustomUserIdentity user = this.Context.CurrentUser as CustomUserIdentity;
                    request.UserId = user.Id;
                }

                object response = null;
                unitOfWork.DoInTransaction(() =>
                {
                    response = messageHandler.Handle(request);
                });

                return(response);
            };
        }
Exemplo n.º 3
0
        public ActionResult <string> Post([FromBody] AuthRequestModel authRequestModel)
        {
            var login    = authRequestModel.Login;
            var password = authRequestModel.Password;

            if (_context.Users.Where(u => u.UserName == login).ToArray().Length == 0)
            {
                var user = new CustomUserIdentity();
                user.UserName     = login;
                user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, password);

                _context.Users.Add(user);
                _context.SaveChanges();

                return("User: "******" added");
            }
            else
            {
                return(BadRequest("User allready exist in Database"));
            }
        }
Exemplo n.º 4
0
        protected AuthenticatedGetModule(IUnitOfWork unitOfWork, IAuthenticatedQueryHandler <TQuery> queryHandler, string path)
        {
            this.unitOfWork   = unitOfWork;
            this.queryHandler = queryHandler;

            this.RequiresHttps();
            this.RequiresAuthentication();

            Get[path] = _ =>
            {
                CustomUserIdentity user  = this.Context.CurrentUser as CustomUserIdentity;
                TQuery             query = this.Bind <TQuery>();
                query.UserId = user.Id;

                object response = null;
                unitOfWork.DoInTransaction(() =>
                {
                    response = queryHandler.Handle(query);
                });

                return(response);
            };
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var avatarFile = Input.AvatarFile;

                var avatarUrl = "";

                if (avatarFile != null)
                {
                    // check image size is not greater than 8mb otherwise skip
                    if (avatarFile.Length / 1024 / 1024 >= 8)
                    {
                        ModelState.AddModelError("Input.AvatarFile", "Image size is too large and has been removed max 8MB");
                        return(Page());
                    }
                    // get random filename and combine with the extension file camewith
                    var uniqueFileName = Path.GetRandomFileName() + Path.GetExtension(avatarFile.FileName);
                    var uploadPath     = Path.Combine(_hostingEnvironment.WebRootPath, "ProductImages");
                    var filePath       = Path.Combine(uploadPath, uniqueFileName);

                    var imgUrl = await _blobService.UploadFileBlobAsync(avatarFile, uniqueFileName);



                    avatarUrl = imgUrl;
                }

                else
                {
                    avatarUrl = $"~/Image/user.svg";
                }


                var user = new CustomUserIdentity {
                    UserName = Input.Username, Email = Input.Email, Avatar = avatarUrl
                };
                var result = await _userManager.CreateAsync(user, Input.Password);



                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }