public static CompletedResult Create(object state, AsyncCallback callback)
        {
            CompletedResult cr = new CompletedResult(state);

            cr.Complete(callback);
            return(cr);
        }
Exemplo n.º 2
0
        private IAsyncResult Sample_BeginAuthenticateRequest(Object source, EventArgs e,
                                                             AsyncCallback cb, Object state)
        {
            IAsyncResult    ar          = null;
            HttpApplication application = (HttpApplication)source;
            HttpContext     context     = application.Context;
            string          path        = context.Request.Url.AbsolutePath;

            if (path.StartsWith(CookiePath, StringComparison.OrdinalIgnoreCase) &&
                (path.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase) ||
                 path.EndsWith("/", StringComparison.Ordinal)))
            {
                RequestInfo info;
                HttpCookie  machCookie = context.Request.Cookies[MachCookie];
                if ((machCookie == null) || !machCookie.HasKeys ||
                    (machCookie.Values[MachId] == null))
                {
                    //
                    // If the client didn't send a machine cookie, then create a new RequestInfo
                    // object, and store it in the per-request Items cache.  The cookie will be
                    // created later.
                    //
                    info = new RequestInfo(Guid.NewGuid(), true, false);
                }
                else
                {
                    string guidStr = machCookie.Values[MachId];
                    try
                    {
                        Guid machGuid = new Guid(guidStr);

                        //
                        // If the client sent a valid machine cookie, check to see if
                        // the first response flag was set in the cookie
                        //
                        bool firstResp = false;
                        if (machCookie.Values[MachFirst] != null)
                        {
                            firstResp = true;
                        }
                        info = new RequestInfo(machGuid, false, firstResp);
                    }
                    catch (FormatException)
                    {
                        //
                        // If the old cookie was malformed, then create and cache the
                        // RequestInfo object and request that a new cookie be created later
                        //
                        info = new RequestInfo(Guid.NewGuid(), true, false);
                    }
                }
                context.Items[RequestInfo.REQ_INFO] = info;

                //
                // If this is the first time the browser has
                // sent the cookie back to us, then record the
                // ID in the database.
                //
                if (info.FirstResponse)
                {
                    //
                    // Insert or update the machine ID asynchronously.  Don't write the full PageView
                    // record until later, in the worker thread.
                    //
                    SqlConnection conn = new SqlConnection(ConnString);
                    SqlCommand    cmd  = new SqlCommand("[Traffic].[AddMachine]", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("id", SqlDbType.UniqueIdentifier).Value = info.MachineId;
                    conn.Open();
                    ar = cmd.BeginExecuteNonQuery(cb, cmd);
                }
            }
            return(ar ?? CompletedResult.Create(state, cb));
        }