Exemplo n.º 1
0
        public async Task <AuthenticateResponse> LoginAsync(AuthenticateRequest model)
        {
            var user = await userRepository.AuthenticateUserAsync(model.OwnerId, model.Username, model.Password);

            if (user == null)
            {
                return(null);
            }

            // authentication successful so generate jwt token
            var userDto = userToUserDtoHydrator.Hydrate(user);
            var token   = await generateJwtToken(userDto);

            await cachingService.CreateUserSession(model.OwnerId, userDto.UserName, token);

            return(new AuthenticateResponse(userDto, token));
        }
Exemplo n.º 2
0
        private static void Listener(object args)
        {
            DuplexPipeChannel channel = (DuplexPipeChannel)args;

            IBus      bus      = channel.bus;
            Stream    stream   = channel.InStream;
            IHydrator hydrator = channel.settings.Hydrator;

            CancellationTokenSource cts   = channel.thrCancelTokens;
            CancellationToken       token = cts.Token;

            while (!token.IsCancellationRequested)
            {
                object payload;
                using (MemoryStream ms = new MemoryStream())
                {
                    bool final;
                    do
                    {
                        byte[] buffer = new byte[HeaderPacketSize];
                        var    handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                        try
                        {
                            ReadBytes(buffer, stream);
                            HeaderPacket header =
                                (HeaderPacket)
                                Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(HeaderPacket));
                            final = header.Final;

                            if (token.IsCancellationRequested)
                            {
                                break;
                            }

                            if (header.Size > 0)
                            {
                                byte[] dataBuffer = new byte[header.Size];
                                var    red        = ReadBytes(dataBuffer, stream);
                                ms.Write(dataBuffer, 0, red);
                            }
                        }
                        finally
                        {
                            handle.Free();
                        }
                    } while (!final && !token.IsCancellationRequested);

                    if (ms.Length == 0)
                    {
                        continue;
                    }
                    ms.Seek(0, SeekOrigin.Begin);

                    try
                    {
                        payload = hydrator.Hydrate(ms);
                    }
                    catch
                    {
                        payload = null;
                    }
                }

                if (payload != null)
                {
                    bus.Publish(payload);
                }
            }
        }