Пример #1
0
        public void ShoudStoreClientRole(Role role)
        {
            _registry.SetClientRole(_client1, role);
            var newRole = _registry.GetRole(_client1);

            newRole.ShouldBe(role);
        }
Пример #2
0
        public async Task <IActionResult> Claim(Role role)
        {
            var client      = (Guid)HttpContext.Items[CookieIdentificationMiddleware.ClientIdKey];
            var currentRole = _registry.GetRole(client);
            var owner       = _registry.GetOwner(role);
            var success     = true;

            if (currentRole == role)
            {
                if (role != Role.Reader)
                {
                    success = TryDefend(client, role);
                }
            }
            else if (!owner.HasValue)
            {
                _registry.SetClientRole(client, role);
            }
            else
            {
                success = await TryClaimThroughDisputeAsync(client, owner.Value, role);
            }

            if (success)
            {
                return(Ok());
            }
            else
            {
                return(Conflict("The role is occupied."));
            }
        }
Пример #3
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            Guid clientId;

            if (!context.Request.Cookies.TryGetValue(ClientIdKey, out string clientIdString))
            {
                clientId = Guid.NewGuid();
                context.Response.Cookies.Append(ClientIdKey, clientId.ToString());
            }
            else
            {
                clientId = Guid.Parse(clientIdString);
            }
            context.Items.Add(ClientIdKey, clientId);
            context.Items.Add(ClientRoleKey, _roleRegistry.GetRole(clientId));
            await next.Invoke(context);
        }