Search code examples
c#asp.netcontent-management-systemkentico

How to authenticate user via Kentico 7.x API in ASP.NET webpart?


I try to create new documents in kentico via API, but I have a problem with user authentication. I try to use the following code:

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

but my authorisation system on the site is not connected with kentico authoristation and it does not work. I tried also put admin username there:

CMS.SiteProvider.UserInfo userInfo =
CMS.SiteProvider.UserInfoProvider.GetUserInfo("administrator");
CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(userInfo);

It also does not work. I found an example to authenticate user in code but it was for Kentico 4.x, now I use kentico 7.x which do not support "AuthenticateUser" method

CMS.SiteProvider.UserInfo ui = CMS.SiteProvider.UserInfoProvider.AuthenticateUser(txtboxUsername.Text,
txtboxPassword.Text, CMS.CMSHelper.CMSContext.CurrentSite.SiteName);

Could you suggest me how can I authenticate user in code?


Solution

  • Why to you think is an authentication problem? The code you posted just instantiates TreeProvider, it does not create documents. I suggest to look at API examples which you can find under Site manager - Support - API Examples - Documents. Basically it will be something like this:

    TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
    
    // Select root at parent
    TreeNode parentNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/", "en-us");
    
    // Create a new instance of the Tree node
    TreeNode newNode = TreeNode.New("CMS.MenuItem", tree);
    
    // Set the document's properties
    newNode.DocumentName = "Document name";
    newNode.DocumentCulture = "en-us";
    
    // Set document type specific fields
    newNode.SetValue("Field1", "value");
    
    // Insert the document into the content tree
    DocumentHelper.InsertDocument(newNode, parentNode, tree);