Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Feb 23, 2011 @ 01:14
    Tom
    0

    Deleting node in codebehind of a page - The user has no umbraco contextid - try logging in

    Hi Guys,

    I have moved a scheduled task into a simple aspx page that I'll post to via curl.. so that i could get an http context for deleting nodes as discussed here

    Now however I am receiving an error stating: The user has no umbraco contextid - try logging in

    So am i correct in assuming I'll need to log an admin user in before running this chunk of code? and if so how would i go about doing that programatically say for example my user in the backend is called "TaskUser" and the password is "password"

    Cheers, Tom

  • Tom 713 posts 954 karma points
    Feb 23, 2011 @ 03:24
    Tom
    0

    I tried this:

    void DeleteItemsForCategory()
            {
                try
                {
                    Document parentNode = new Document(CurrentCategoryId);//use id of parent node 
                    foreach (Document child in parentNode.Children)
                    {
                        child.delete();
                        umbraco.library.UpdateDocumentCache(child.Id);
                    }
                    umbraco.library.RefreshContent();
                }
                catch (Exception ex)
                {
                    ExceptionReporter.ReportException(ex, "Exception Occurred Trying To Delete Items From Category");
                    throw ex;
                }
            }
    void CheckUserLogin()
            {
                if (HttpContext.Current.Response.Cookies["UserContext"] == null || string.IsNullOrWhiteSpace(HttpContext.Current.Response.Cookies["UserContext"].Value))
                {
                    if (Membership.Providers[UmbracoSettings.DefaultBackofficeProvider].ValidateUser("ServiceUser", "h3lt3rSkelTa*"))
                    {
                        umbraco.BusinessLogic.User u = new User("ServiceUser");
                        Guid userContextId = Guid.NewGuid();
                        SqlHelper.ExecuteNonQuery("insert into umbracoUserLogins (contextID, userID, timeout) values (@contextId,'" + u.Id + "','" + (DateTime.Now.Ticks + (_ticksPrMinute * _umbracoTimeOutInMinutes)).ToString() +
                                                              "') ",
                                                              SqlHelper.CreateParameter("@contextId", userContextId));
                        HttpCookie cookie = new HttpCookie("UserContext");
                        cookie.Name = "UserContext";
                        cookie.Value = userContextId.ToString();
                        cookie.Expires = DateTime.Now.AddDays(1);
                        HttpContext.Current.Response.Cookies.Add(cookie);
                    }
                }
            }
    if (itemsFromCsv.Count > 0)
                                        {
                                            CheckUserLogin();
                                            DeleteItemsForCategory();
                                            InsertItemsFromCsvLine(itemsFromCsv);
                                        }
    protected override void InsertItemsFromCsvLine(List<CmsProductItem> itemsFromCsvLine)
            {
                foreach (var itemFromCsvLine in itemsFromCsvLine)
                {
                    try
                    {
                        DocumentType dt = DocumentType.GetByAlias("product");
                        User author = User.GetUser(0);
                        Document doc = Document.MakeNew(itemFromCsvLine.Code, dt, author, CurrentCategoryId);
    
                        doc.getProperty("productID").Value = itemFromCsvLine.ID;
                        doc.getProperty("productAddedDate").Value = itemFromCsvLine.ItemIsNew ? DateTime.Now : DateTime.Now.AddYears(-1);
                        doc.getProperty("productCode").Value = itemFromCsvLine.Code;
                        doc.getProperty("productDescription").Value = itemFromCsvLine.Description;
                        doc.getProperty("productSize").Value = itemFromCsvLine.Dimension;
                        doc.getProperty("productRRP_AU").Value = !string.IsNullOrWhiteSpace(itemFromCsvLine.AURetail) ? itemFromCsvLine.AURetail : null;
                        doc.getProperty("productRRP_NZ").Value = !string.IsNullOrWhiteSpace(itemFromCsvLine.NZRetail) ? itemFromCsvLine.NZRetail : null;
                        doc.getProperty("productRRP_SG").Value = !string.IsNullOrWhiteSpace(itemFromCsvLine.SGRetail) ? itemFromCsvLine.SGRetail : null;
    
                        doc.Publish(author);
                        umbraco.library.UpdateDocumentCache(doc.Id);
                    }
                    catch (Exception ex)
                    {
                        ex.Data["currentProduct"] = itemFromCsvLine.ID;
                        throw ex;
                    }
    
                    InsertLogger.AddLine(itemFromCsvLine.Code + " (" + itemFromCsvLine.ID + ")" + (itemFromCsvLine.ItemIsNew ? " ITEM IS NEW!" : ""));
                    InsertLogger.AddBlankLine(2);
                }
            }

    But because the cookie is set in the single response umbraco hasn't picked it up and still thinks there is no auth

  • Richard Soeteman 4047 posts 12900 karma points MVP 2x
    Feb 23, 2011 @ 07:49
    Richard Soeteman
    0

    Hi,

    The Umbraco webservices are using the validateCredentials method on the User object, maybe you can use that?

    BusinessLogic.

     

    User.validateCredentials(Login, Password)

    Cheers,

    Richard

  • Tom 713 posts 954 karma points
    Feb 23, 2011 @ 23:51
    Tom
    0

    Hi Richard.. using reflector it just looks like validateCredentials hits the db to check if the username and password are valid.. good check though.. ill poke around in reflector with the webservices and give it a look:

    public static bool validateCredentials(string lname, string passw, bool checkForUmbracoConsoleAccess)
    {
        string consoleCheckSql = "";
        if (checkForUmbracoConsoleAccess)
        {
            consoleCheckSql = "and userNoConsole = 0 ";
        }
        object tmp = SqlHelper.ExecuteScalar<object>("select id from umbracoUser where userDisabled = 0 " + consoleCheckSql + " and userLogin = @login and userPassword = @pw", new IParameter[] { SqlHelper.CreateParameter("@login", lname), SqlHelper.CreateParameter("@pw", passw) });
        if (tmp == null)
        {
            Log.Add(LogTypes.LoginFailure, GetUser(0), -1, "Login: '" + lname + "' failed, from IP: " + HttpContext.Current.Request.UserHostAddress);
        }
        return (tmp != null);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft