Esempio n. 1
0
        public void Start(CancellationToken cancellationToken)
        {
            async Task UpdateHandle()
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    var update = await _incomingUpdateQueueReader.DequeueAsync(cancellationToken);

                    await _updateService.SaveUpdateAsync(update, cancellationToken);

                    var updateContext = new UpdateContext(this, update);
                    await using var innerScope = _lifetimeScope.BeginLifetimeScope(
                                    typeof(UpdateContext),
                                    builder => { builder.RegisterInstance(updateContext).As <IUpdateContext>(); });

                    //await innerScope.Resolve<MessageHandler>().UpdateHandleAsync(cancellationToken);
                    await innerScope.Resolve <IUpdateHandler>().Execute(cancellationToken);

                    await _updateService.MakeUpdateProcessedAsync(update, cancellationToken);
                }
            }

            async Task RequestHandle()
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    var request = await _outgoingRequestQueueReader.DequeueAsync(cancellationToken);

                    await _requestDispatcher.DispatchAsync(request, cancellationToken);
                }
            }

            async Task RestartOnExceptionAsync(Func <Task> task)
            {
                do
                {
                    try
                    {
                        await task();

                        break;
                    }
                    catch (ChannelClosedException ex)
                    {
                        _logger.LogInformation(ex, $"Channel for user {User.Id} has been closed");
                        break;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Get exception during update or request handle");
                    }
                } while (true);
            }

            _logger.LogInformation($"Session for identityUser {User.Id} started");

            _updateHandleTask  = Task.Run(() => RestartOnExceptionAsync(UpdateHandle), cancellationToken);
            _requestHandleTask = Task.Run(() => RestartOnExceptionAsync(RequestHandle), cancellationToken);
        }
Esempio n. 2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            string errorMessage = "Погрешена лозинка или корисничко име";
            var    user         = await _requestDispatcher.DispatchAsync(new GetUserByUserName.Request
            {
                UserName = context.UserName,
                Password = context.Password
            });

            if (user.Response == null || user.HasErrors)
            {
                context.SetError(errorMessage);
            }
            else
            {
                if (user.Response.Lozinka == context.Password)
                {
                    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim(ClaimTypes.Name, user.Response.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
                    identity.AddClaim(new Claim("username", user.Response.UserName));
                    identity.AddClaim(new Claim("userId", user.Response.ID.ToString()));
                    var props = new AuthenticationProperties(new Dictionary <string, string>
                    {
                        {
                            "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                        },
                        {
                            "userName", context.UserName
                        },
                        {
                            "userId", user.Response.ID.ToString()
                        }
                    });
                    var ticket = new AuthenticationTicket(identity, props);
                    context.Validated(ticket);
                }
                else
                {
                    context.SetError(errorMessage);
                }
            }
        }