Esempio n. 1
0
        internal static string Encode(AnonymousIdData data)
        {
            if (data == null)
            {
                return(null);
            }

            if (data.AnonymousId == null)
            {
                throw new ArgumentException("No anonymous id set in data; make sure you've not used an expiration data in the past", nameof(data));
            }

            byte[] bufferId       = Encoding.UTF8.GetBytes(data.AnonymousId);
            byte[] bufferIdLenght = BitConverter.GetBytes(bufferId.Length);
            byte[] bufferDate     = BitConverter.GetBytes(data.ExpireDate.ToFileTimeUtc());
            byte[] buffer         = new byte[12 + bufferId.Length];

            Buffer.BlockCopy(bufferDate, 0, buffer, 0, 8);
            Buffer.BlockCopy(bufferIdLenght, 0, buffer, 8, 4);
            Buffer.BlockCopy(bufferId, 0, buffer, 12, bufferId.Length);

            return(Base64UrlEncoder.Encode(buffer));
        }
        public void HandleRequest(HttpContext httpContext)
        {
            string   encodedValue;
            bool     isAuthenticated = httpContext.User.Identity.IsAuthenticated;
            DateTime now             = DateTime.UtcNow;

            // Handle secure cookies over an unsecured connection
            if (cookieBuilder.SecurePolicy == CookieSecurePolicy.Always && !httpContext.Request.IsHttps)
            {
                encodedValue = httpContext.Request.Cookies[cookieBuilder.Name];
                if (!string.IsNullOrWhiteSpace(encodedValue))
                {
                    httpContext.Response.Cookies.Delete(cookieBuilder.Name);
                }

                // Adds the feature to request collection
                httpContext.Features.Set <IAnonymousIdFeature>(new AnonymousIdFeature());

                return;
            }

            // Gets the value and anonymous Id data from the cookie, if available
            encodedValue = httpContext.Request.Cookies[cookieBuilder.Name];
            AnonymousIdData decodedValue = AnonymousIdEncoder.Decode(encodedValue);

            string anonymousId = null;

            if (decodedValue != null && !string.IsNullOrWhiteSpace(decodedValue.AnonymousId))
            {
                // Copy the existing value in Request header
                anonymousId = decodedValue.AnonymousId;

                // Adds the feature to request collection
                httpContext.Features.Set <IAnonymousIdFeature>(new AnonymousIdFeature()
                {
                    AnonymousId = anonymousId
                });
            }

            // User is already authenticated
            if (isAuthenticated)
            {
                return;
            }

            // Don't create a secure cookie in an unsecured connection
            if (cookieBuilder.SecurePolicy == CookieSecurePolicy.Always && !httpContext.Request.IsHttps)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(anonymousId))
            {
                // Creates a new identity
                anonymousId = Guid.NewGuid().ToString();

                // Adds the feature to request collection
                httpContext.Features.Set <IAnonymousIdFeature>(new AnonymousIdFeature()
                {
                    AnonymousId = anonymousId
                });
            }
            else
            {
                // Sliding expiration is not required for this request
                if (decodedValue != null &&
                    decodedValue.ExpireDate > now &&
                    decodedValue.ExpireDate - now > cookieBuilder.Expiration / 2)
                {
                    return;
                }
            }

            // Appends the new cookie
            CookieOptions   options = cookieBuilder.Build(httpContext);
            AnonymousIdData data    = new AnonymousIdData(anonymousId, options.Expires.Value.DateTime);

            encodedValue = AnonymousIdEncoder.Encode(data);
            httpContext.Response.Cookies.Append(cookieBuilder.Name, encodedValue, options);
        }